写代码有一年了,深深的感觉到枚举非常灵活。标记可数的状态是往往都少不了它。它是字符串和整形数值对应的完美桥梁。下面通过代码来展现这一良好的特性吧。
建立相应的枚举:
public enum EState
{
Sit=100,//坐下
Stand=101,//站立
Run=102,//奔跑
}
下面测试代码:
EState currentState = (EState)101;
Console.WriteLine ("int to enum");
Console.WriteLine ("Current State is :string value: {0},int value:{1}",currentState,(int)currentState);
currentState = (EState)Enum.Parse (typeof(EState), "Stand");
Console.WriteLine ("string to enum");
Console.WriteLine ("Current State is :string value: {0},int value:{1}",currentState,(int)currentState);
运行结果我已经以图片的形式上传了。小伙伴感受一下枚举的魅力吧。