Enumeration is a set of named integer constants. Enumerated type is to use the enum keyword declared.
C # enumeration is a value type. In other words, the enumeration contains its own value and can not inherit or transfer inheritance.
First, the definition of enumeration
Each symbol represents an integer enumerated list of values, a greater than its preceding signed integer values. By default, the first value is 0 enumeration symbols.
1 public enum Days 2 { 3 Monday = 0, 4 Tuesday, 5 Wednesday, 6 Thursday = 10, 7 Friday, 8 Saturday, 9 Sunday 10 } 11 12 static void Main(string[] args) 13 { 14 Console.WriteLine((int)Days.Tuesday); 15 Console.WriteLine((int)Days.Friday); 16 Console.ReadLine(); 17 }
Enumeration type of each element is based int, may be used colons indicate another type integer. Approved enumeration types: byte, sbyte, short, ushort , int, uint, long, ulong
1 public enum Days : byte 2 { 3 Monday, 4 Tuesday, 5 Wednesday, 6 Thursday, 7 Friday, 8 Saturday, 9 Sunday 10 }
Second, conversions between enum, int, string three types
1 public enum Gender 2 { 3 男, 4 女 5 } 6 7 static void Main(string[] args) 8 { 9 Console.WriteLine("enum ---> int : " + (int)Gender.男); 10 11 Console.WriteLine("enum ---> string : " + Convert.ToString(Gender.男)); 12 Console.WriteLine("enum ---> string : " + Gender.女.ToString()); 13 14 Console.WriteLine("int ---> enum : " + (Gender)0); 15 16 Console.WriteLine("int ---> string : " + Enum.GetName(typeof(Gender), 0)); 17 18 Console.WriteLine("string ---> enum :" + Enum.Parse(typeof(Gender), "男")); 19 20 Console.ReadLine(); 21 }
Third, the cycle enum
1 static void Main(string[] args) 2 { 3 foreach (var gender in Enum.GetValues(typeof(Gender))) 4 { 5 Console.WriteLine(gender.ToString() + "=" + (int)gender); 6 } 7 Console.ReadLine(); 8 }
Fourth, get a description of the enum
1 public enum GenderDesc 2 { 3 [Description("male")] 4 男, 5 [Description("female")] 6 女 7 } 8 9 static void Main(string[] args) 10 { 11 Console.WriteLine("女 描述信息 : " + (Description(Gender.女) ?? "null")); 12 Console.WriteLine("Female description: " + (. The Description (GenderDesc F) ?? " null " )); 13 is 14 Console.ReadLine (); 15 } 16 . 17 /// <Summary> 18 is /// Gets an enumeration value descriptive text . 19 /// </ Summary> 20 is /// <param name = "E"> enumeration value </ param> 21 is /// <Returns> </ Returns> 22 is public static String the Description ( the this the enum E) 23 is { 24 the Type of enumType = e.GetType (); 25 var fieldInfo = enumType.GetFields().FirstOrDefault(a => a.Name == Enum.GetName(enumType, e)); 26 object[] obj = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); 27 if (obj == null || obj.Length == 0) return null; 28 29 DescriptionAttribute des = (DescriptionAttribute)obj[0]; 30 return des.Description; 31 }
Fifth, the enumeration method of converting a set of dictionary
1 public enum Gender 2 { 3 男, 4 女 5 } 6 7 static void Main(string[] args) 8 { 9 Console.WriteLine("int ---> string : " + EnumHelper.GetEnumName<Gender>(1)); 10 11 var dic1 = EnumHelper.getEnumDic<Gender>(); 12 Console.WriteLine("enum ---> dictionary : "); 13 foreach (var item in dic1) 14 { 15 Console.WriteLine(item.Key + "==" + item.Value); 16 } 17 18 Console.WriteLine("enum ---> dictionary : "); 19 var dic = EnumHelper.GetDic<Gender>(); 20 foreach (var item in dic) 21 { 22 Console.WriteLine(item.Key + "==" + item.Value); 23 } 24 25 Console.ReadLine(); 26 }
. 1 public static class EnumHelper 2 { . 3 /// <Summary> . 4 /// Gets an enumeration name based on the values enumerated . 5 /// </ Summary> . 6 /// <typeParam name = "T"> enum type </ typeParam> . 7 /// <param name = "Status"> enumerated value </ param> . 8 /// <Returns> </ Returns> . 9 public static String the GetEnumNameå <T> ( the this int Status) 10 { 11 return the Enum.GetName(typeof(T), status); 12 is } 13 is /// <Summary> 14 /// Gets an enumeration name set 15 /// </ Summary> 16 /// <typeParam name = "T"> </ typeParam> . 17 /// <Returns> < / Returns> 18 is public static String [] GetNamesArr <T> () . 19 { 20 is return Enum.GetNames ( typeof (T)); 21 is } 22 is /// <Summary> 23 is /// enumerated is converted into a set of dictionary 24 /// </ Summary> 25 /// <typeParam name = "T">枚举类型</typeparam> 26 /// <returns></returns> 27 public static Dictionary<string, int> getEnumDic<T>() 28 { 29 30 Dictionary<string, int> resultList = new Dictionary<string, int>(); 31 Type type = typeof(T); 32 var strList = GetNamesArr<T>().ToList(); 33 foreach (string key in strList) 34 { 35 string val = Enum.Format(type, Enum.Parse(type, key), "d"); 36 resultList.Add(key, int.Parse(val)); 37 } 38 return resultList; 39 } 40 /// <summary> 41 /// 将枚举转换成字典 42 /// </summary> 43 /// <typeparam name="TEnum"></typeparam> 44 /// <returns></returns> 45 public static Dictionary<string, int> GetDic<TEnum>() 46 { 47 Dictionary<string, int> dic = new Dictionary<string, int>(); 48 Type t = typeof(TEnum); 49 var arr = Enum.GetValues(t); 50 foreach (var item in arr) 51 { 52 dic.Add(item.ToString(), (int)item); 53 } 54 55 return dic; 56 } 57 }
Fifth, the enumeration summary
Use enum can avoid HardCode when coding, to effectively improve readability and scalability of the code, of course, the latter can also be a lot easier to maintain.
Sixth, reference articles
https://www.cnblogs.com/BluceLee/p/8989479.html