枚举是直接在命名空间、类或结构中使用 enum 关键字定义的。所有常量名都可以在大括号内声明,并用逗号分隔。
枚举可以是任何数字数据类型,例如 byte,sbyte,short,ushort,int,uint,long 或 ulong。但是,枚举不能为字符串类型。
- 枚举(Enum)定义
public enum WeekDays { Monday = 0, Tuesday = 1, Wednesday = 2, Thursday = 3, Friday = 4, Saturday = 5, Sunday = 6 }
- 枚举值获取
Console.WriteLine(Enum.GetName(typeof(WeekDays), 4));//输出:Friday Console.WriteLine(WeekDays.Friday);//输出:Monday Console.WriteLine((int) WeekDays.Friday);//输出:4 foreach (var item in Enum.GetValues(typeof(WeekDays))) { Console.WriteLine((int) item);//循环输出:0,1... } foreach (var item in Enum.GetNames(typeof(WeekDays))) { Console.WriteLine(item);//循环输出:Monday,Tuesday... }
- Description、EnumMember的使用,完整实列
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; namespace Study { public class MyEnum { public enum WeekDays { [EnumMember(Value = "周一,还有四天.... ")] Monday = 0, [Description("周二,还有三天....")] Tuesday = 1, Wednesday = 2, Thursday = 3, Friday = 4, Saturday = 5, Sunday = 6 } enum MemberLevel : long { gold = 2147483648L, silver = 232L, copper = 10L } public MyEnum() { Employ(WeekDays.Friday); } public static void Employ(WeekDays weekDays) { switch (weekDays) { case WeekDays.Monday: break; case WeekDays.Tuesday: break; case WeekDays.Wednesday: break; case WeekDays.Thursday: break; case WeekDays.Friday: Console.WriteLine(Enum.GetName(typeof(WeekDays), 4));//输出:Friday Console.WriteLine(WeekDays.Friday);//输出:Monday Console.WriteLine((int)WeekDays.Friday);//输出:4 foreach (var item in Enum.GetValues(typeof(WeekDays))) { Console.WriteLine((int)item);//循环输出:0,1... } foreach (var item in Enum.GetNames(typeof(WeekDays))) { Console.WriteLine(item);//循环输出:Monday,Tuesday... } Console.WriteLine(WeekDays.Monday.ToEnumMemberAttrValue());//输出:周一,还有四天.... Console.WriteLine(WeekDays.Tuesday.GetDescription());//输出:周二,还有三天.... break; case WeekDays.Saturday: break; case WeekDays.Sunday: break; default: break; } } } public static class EnumExt { public static string ToEnumMemberAttrValue(this Enum @enum) { var attr = @enum.GetType().GetMember(@enum.ToString()).FirstOrDefault()?. GetCustomAttributes(false).OfType<EnumMemberAttribute>(). FirstOrDefault(); if (attr == null) return @enum.ToString(); return attr.Value; } public static string GetDescription(this Enum @enum, bool isTop = false) { Type enumType = @enum.GetType(); DescriptionAttribute attr = null; if (isTop) { attr = (DescriptionAttribute)Attribute.GetCustomAttribute(enumType, typeof(DescriptionAttribute)); } else { // 获取枚举常数名称。 string name = Enum.GetName(enumType, @enum); if (name != null) { // 获取枚举字段。 FieldInfo fieldInfo = enumType.GetField(name); if (fieldInfo != null) { // 获取描述的属性。 attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute; } } } if (attr != null && !string.IsNullOrEmpty(attr.Description)) return attr.Description; else return string.Empty; } } }