一、在学习枚举之前,首先来听听枚举的优点。
二、枚举说明
C#提供类一个类来方便操作枚举,下面给出这个类的常用方法:
| 方法 | 名称 |
| CompareTo | 将此实例与指定对象进行比较并返回一个对二者的相对值的指示 |
| Equals | 指示此实例是否等于指定的对象 |
| Format | 根据指定格式将指定枚举类型的指定值转换为其等效的字符串表示形式 |
| GetName | 在指定枚举中检索具有指定值的常数的名称 |
| GetNames | 检索指定枚举中常数名称的数组 |
| GetTypeCode | 返回此实例的基础 TypeCode |
| GetUnderlyingType | 返回指定枚举的基础类型 |
| GetValues | 索指定枚举中常数值的数组 |
| HasFlag | 确定当前实例中是否设置了一个或多个位域 |
| IsDefined | 返回指定枚举中是否存在具有指定值的常数的指示 |
| Parse | 将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象。 一个参数指定该操作是否不区分大小写 |
| TryParse | 将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象。 用于指示转换是否成功的返回值 |
enum sex : byte //显示指定枚举的底层数据类型
{
male,
female, //此逗号可以省略
}; 显式设置枚举的成员常量值,默认是从0开始,逐个递增的。但是以下例子却设置成了1,2,3,4,5,6,7,0。而且成员值可以一样的。
enum Week
{
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Sunday = 0,
Everyday = 1 //成员的值可以设置成一样的,但是成员不行
}
Console.WriteLine((int)Week.Monday); //获取值 示例,由枚举值获取枚举名称与由枚举名称获取枚举值
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Enum.GetName(typeof(Man),1)); //还是 刘备 (由值获取名字)
string[] array1 = Enum.GetNames(typeof(Man));
Console.WriteLine(array1[1]); //关羽
Array array2 = Enum.GetValues(typeof(Man));
Console.WriteLine(array2.GetValue(1)); //还是关羽
Type t = Enum.GetUnderlyingType(typeof(Man));
Console.WriteLine(t); //输出 Int32
//由值获取内容
int i = 1;
string Name = Enum.Parse(typeof(Man), i.ToString()).ToString(); //此时 Name="刘备"
Console.WriteLine(Name);
//由值获取内容
string Name2 = "关羽";
int j = Convert.ToInt32(Enum.Parse(typeof(Man), Name2)); //此时 j=2
Console.WriteLine(j);
Console.ReadKey();
}
}
enum Man
{
刘备 = 1,
关羽 = 2,
张飞 = 3
}
class Program
{
static void Main(string[] args)
{
var man = Week.白 | Week.美; //赋值为101 计算方法001或上100,结果是101
Console.WriteLine((int)man);
if ((man & Week.白) == Week.白) //101 man
{ //001 白 逐位相与,
Console.WriteLine("此人白"); //001 如果结果是白,就可以反推出man包含 白
}
else
{
Console.WriteLine("此人黑");
}
Console.ReadKey();
}
}
[System.Flags]
public enum Week
{
白 = 1, //001
富 = 2, //010
美 = 4, //100
}
select * from Table1 where Tag & 1 = 1 //Tag是列名 select * from Table1 where Tag | 1 = Tag
namespace MvcStart.Controllers
{
public class HomeController : Controller
{
public ActionResult GetSexList()
{
Dictionary<string, int> Sexlist = new Dictionary<string, int>();
string[] keys = Enum.GetNames(typeof(sex));
Array values = Enum.GetValues(typeof(sex));
for (int i = 0; i < keys.Length; i++)
{
Sexlist.Add(keys[i], (int)values.GetValue(i));
}
return View(Sexlist);
}
}
public enum sex
{
male = 1,
female = 2,
other = 3
}
}
@model Dictionary<string, int>
<select>
@foreach(var item in @Model)
{
<option value="@item.Value">@item.Key</option>
}
</select>
<select>
<option value="1">male</option>
<option value="2">female</option>
<option value="3">other</option>
</select>
public static class GetDescription
{
/// <summary>
/// 获取描述信息
/// </summary>
/// <param name="en"></param>
/// <returns></returns>
public static string description(this Enum en)
{
Type type = en.GetType();
MemberInfo[] memInfo = type.GetMember(en.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
return ((DescriptionAttribute)attrs[0]).Description;
}
return en.ToString();
}
}
public enum Sex
{
[Description("男")]
man = 1,
[Description("女")]
woman = 2,
[Description("其他")]
other = 3
}