关于enum的一些常用方法

今天在开发的过程中,需要用到Enum,就顺便查些资料,做下简单记录.

定义一个enum

enum Colors{ Red, Green, Blue, Yellow};

enum Colors{ Red=1, Green=2, Blue=3, Yellow=4};

1.得到枚举中常数值的数组。

for each(int i in Enum.GetValues(typeof(Colors)))
Console.WriteLine(i);

2. 检索指定枚举中常数名称的数组

for each(string s in Enum.GetNames(typeof(Colors)))
Console.WriteLine(s);

3.转换Integer常数值和转换一个String常数名到Eumn实例

 

public Colors GetCoorsInstance(string value){

    Colors returnValue = (Colors)Enum.Parse(typeof(Colors), value);
    return returnValue;

}

原文地址:https://www.cnblogs.com/ghfsusan/p/1414226.html