C#笔记之枚举转换

需求:通过枚举类型的索引值获得枚举值

实现:

		public static ScoreType GetScoreType(int index)//通过枚举类型的索引值获得枚举值
        {
            ScoreType scoreType = ScoreType.None;

            foreach (ScoreType score in Enum.GetValues(typeof (ScoreType)))//Enum.GetValues(typeof (ScoreType))获得ScoreType枚举的所有内容(即一个数组array)
            {
                if (index == (int) score)
                {
                    scoreType = score;
                    break;
                }
            }

            return scoreType;
        }

需求:枚举值转int

实现:这个没啥好说的,直接强转成int就完事了

需求:枚举值转string(也就是获得当前枚举值的名字)

实现:

		public static string GetEnumName<T>(int index) where T : Enum
        {
            return Enum.GetName(typeof (T), index);
        }

参考自:
C#
添加链接描述

posted @ 2022-07-03 14:13  一世癫狂  阅读(19)  评论(0)    收藏  举报  来源