The original: C # enum,int,string conversion to each other
"Conversion of C # enum,int,string to each other "
1, enum-->string
(1) using the Object.ToString () method: The value of Colors.Green.ToString() is the "Green" string, and (2) The static method of using the enum GetName and GetNames: public static string GetName (type enumtype,object value) public static string[] GetNames (type enumtype) For example: Enum.getname (typeof (Colors), 3)) and Enum.getname (typeof (Colors), Colors.blue ) are all "Blue" values Enum.getnames (typeof (Colors)) Returns an array of enumeration strings.
2, String-->enum
(1) static method of using Enum parse: public static Object parse (Type enumtype,string value) For example:(Colors) Enum.parse ( typeof (Colors), "Red")
3, Enum-->int
(1) Because the base type of the enumeration is an integral type other than Char, it is possible to cast. For example:(int) colors.red, (byte) colors.green
4, Int-->enum
(1) You can cast an integral type to an enumeration type. For example: Colors color = (Colors) 2, then color is colors.blue (2) using the static method of the enum Toobject. Public static Object toobject (Type enumtype,int value) (Colors) Enum.toobject (typeof (Colors), 2), then color is Colors.blue
5, determine whether an integral type is defined in the enumeration method: Enum.isdefined
public static bool IsDefined (Type enumtype,object value) For example:enum.isdefined (typeof (Colors), n))
Reference:
1, Http://msdn.microsoft.com/zh-cn/library/system.enum (v=vs.110). aspx
2, http://www.cnblogs.com/myx/archive/2011/06/17/Enum-Int-String.html
Conversion of C # enum,int,string to each other