在编码中,通常我们会用Enum来定义名称常量表示某些状态,这样让程序代码可读性提高。最近对.net对Enum类型进行了复习。
几个要点:
1.可以显式将枚举类型的变量转换整型。
2.可以对一个枚举变量进行递增或递减操作。
3.定义枚举时手工指定自己想要的整数值。
4.可以选择除int之外的其他类型定义枚举类型的值,如byte,long.
5.所有的Enum派生自Object,Object.ToString()已经自动为每个枚举进行了覆写,作用是返回枚举常数定义时的名称字符串。
例如String转Enum, 可以用Enum.Parse方法。以及.net 3.5 System.Data.DataSetExtensions 对DataRow进行了扩展,支持泛型。
避免装拆箱,提高性能。代码如下:
[Test]
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/// <summary>
/// Enum Knowledge review
/// <remarks>
/// petter liu http://wintersun.cnblogs.com
/// </remarks>
/// </summary>
public
void
Alternative()
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//Enum类型的值默认是int,默认从0开始,所以此处UnDeliver为0
Console.WriteLine((int)Status.UnDeliver);
//设定某个元素的值时,其后元素递增,此处Deliver为2
Console.WriteLine((int)Status.Deliver);
//Recevice 为3
Console.WriteLine((int)Status.Recevice);
Console.WriteLine("------------------");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//define a DataTable
DataTable dt = new DataTable("testname");
DataColumn column=new DataColumn("Status",System.Type.GetType("System.String"));
dt.Columns.Add(column);
DataRow row = dt.NewRow();
row[0] = Status.UnDeliver;
dt.Rows.Add(row);
Status result;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//从字符串转回枚举
result = (Status)Enum.Parse(typeof(Status), row[0].ToString());
Console.WriteLine(result);
Console.WriteLine((int)result);
Console.WriteLine("------------------");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//以对象数组的形式返回枚举中的所有值
foreach(object o in Enum.GetValues(typeof(Status)))
Console.WriteLine(o.ToString());
Console.WriteLine("------------------");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//返回一个枚举类型中所有值的名称字符串数组
foreach (string s in Enum.GetNames(typeof(Status)))
Console.WriteLine(s);
Console.WriteLine("------------------");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Status stauts = Status.Fail;
//G返回 名称
string s1 = Enum.Format(typeof(Status), stauts, "G");
//D返回 值
string s2 = Enum.Format(typeof(Status), stauts, "D");
Console.WriteLine(s1+" "+s2);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
[Test]
public
void
DataRowNormal()
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
DataTable dt = new DataTable("testname");
DataColumn column = new DataColumn("Status",typeof(Status));
dt.Columns.Add(column);
DataRow row = dt.NewRow();
row[0] = Status.UnDeliver;
dt.Rows.Add(row);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Status result;
result = (Status)row[0];
Console.WriteLine(result);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
[Test]
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/
/// <summary>
/// .net 3.5 System.Data.DataSetExtensions 对DataRow进行了扩展,支持泛型
/// <remarks>
/// petter liu http://wintersun.cnblogs.com
/// </remarks>
/// </summary>
public
void
DataRowGeneric()
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
DataTable dt = new DataTable("testname");
DataColumn column = new DataColumn("Status", typeof(Status));
dt.Columns.Add(column);
DataRow row = dt.NewRow();
//System.Data.DataSetExtensions.SetField<T>
row.SetField<Status>(0,Status.UnDeliver);
dt.Rows.Add(row);
//System.Data.DataSetExtensions.Field<T>
Status result= row.Field<Status>(0);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine(result.ToString("d"));
Console.WriteLine(result.ToString("G"));
Console.WriteLine(Enum.GetName(typeof(Status), result));
}
后记,还有就是位域,一个枚举的实例包含多个枚举值。在上面加上System.FLAGs的Attribute.NET Framework中的ThreadState就是
这个实现的,二进制标志枚举。