Turn from: C # enum,int,string conversion to each other
C # enum,int,string The reciprocal conversion enum provides a base class for enumerations whose underlying type can be any integral type except Char. If you do not explicitly declare an underlying type, use Int32. Programming languages typically provide syntax to declare enumerations that consist of a set of named constants and their values. Note: The base type of an enumeration type is any integral type except Char, so the value of the enumeration type is an integer value. Enum provides some practical static methods: (1) methods for comparing instances of an enumeration class (2) to convert the value of an instance to its string representation (3) to convert the string representation of a number to an instance of this class (4) to create a method that specifies an instance of the enumeration and value. Example: Enum Colors {Red, Green, Blue, Yellow}; Enum-->string (1) uses the Object.ToString () method: The value of Colors.Green.ToString () is a "Green" string, and (2) uses the static method of the enum GetName and GetNames: public static string GetName (type enumtype,object value) public static string[] GetNames (type Enumtype) For example: Enum.getnam The values of E (typeof (Colors), 3)) and Enum.getname (typeof (Colors), colors.blue) are all "Blue" Enum.getnames (typeof (Colors)) An array of enumerated strings is returned. String-->enum (1) parse:public static Object Parse (Type enumtype,string value) by using an Enum method, for example: (Colors) Enum.parse (typ EOF (Colors), "Red") enum-->int (1) You can cast because the base type of the enumeration is an integral type other than Char. For example: (int) colors.red, (byte) Colors.green int-->enum (1) can cast to convert an integral type to an enumeration type. For example: Colors color = (Colors) 2, then color is Colors.blue (2)Toobject with the static method of enum. public static Object Toobject (Type enumtype,int value) For example: Colors color = (Colors) enum.toobject (typeof (Colors), 2), then Colo R is the method for Colors.blue to determine whether an integral type is defined in an enumeration: Enum.isdefinedpublic static bool IsDefined (Type enumtype,object value) Example: enum.isdefined (typeof (Colors), N))
Conversion of C # enum,int,string to each other