An enum provides a base class for an enumeration 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.
The Enum provides some useful static methods:
(1) Methods to compare instances of an enumeration class
(2) method of converting the value of an instance to its string representation
(3) method to convert the string representation of a number to an instance of this class
(4) A method that creates an instance of the specified enumeration and value.
Example: Enum Colors {Red, Green, Blue, Yellow};
Enum–>string
(1) using the Object.ToString () method: The value of Colors.Green.ToString () is a "Green" string;
(2) using the static method of enum GetName and GetNames:
publicstaticstringGetNamevalue);publicstaticstringGetNames(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.
String–>enum
(1) The static method of using Enum, parse:
public static Object Parse (Type enumtype,string value)
For example:
(Colors)System.Enum.Parse(typeof"Red");
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;这里写代码片
Int–>enum
(1) You can cast an integral type to an enumeration type.
For example:
color = (Colors)2//,那么color即为Colors.Blue
(2) using the static method of enum Toobject.
public static Object Toobject (Type enumtype,int value)
For example:
Colors color = (Colors)System.Enum.ToObject(typeof2);//,那么color即为Colors.Blue
method to determine whether an integral type is defined in an enumeration: enum.isdefined
public static bool IsDefined (Type enumtype,object value)
For example:
System.Enum.IsDefined(typeof(Colors), n));
u3d enum string int conversions to each other