C# enumeration learning needs note:
The system. Enum type is an abstract base class for all enumerated types (it is a distinct type from the base type of an enumerated type), and members inherited from system. Enum are available in any enumerated type.
There is a boxed conversion from any enumeration type to system. Enum, and there is an unboxed conversion from system. Enum to any enumeration type.
System.Enum is not an enumeration type. Instead, it is a class type from which all enumerated types are derived. The type system. Enum is derived from the type system. ValueType, which in turn is derived from the type object. At run time, the value of type system. Enum can be null or a reference to a boxed value of any enumerated type.
Advantages of C# enumeration:
Enumerations make code easier to maintain and help ensure that valid, expected values are assigned to variables.
Enumerations make the code cleaner, allowing integer values to be represented by descriptive names rather than ambiguous ones.
Enumerations make the code easier to type. When assigning an instance of an enumeration type, the VS.NET IDE uses IntelliSense to pop up a list of acceptable values, reducing keystroke times and allowing us to recall possible values
C# enumerations
Enum. The Parse () method
This method takes three arguments, the first of which is the enum type to use. The syntax is the keyword Typeof followed by the enumeration class name in parentheses. The second argument is the string to be converted, and the third argument is a bool that specifies whether case is ignored when converting. Finally, note that the Enum.parse () method actually returns an object reference — we need to explicitly convert this string to the desired enumerated type (this is an example of unboxing).
The Enum.GetName() method gets the name corresponding to an Enum value
The Enum.GetValues() method gets all the values of the enumeration
Enum.GetNames (typeof) Gets all the names of the enumeration
foreach(string temp in Enum.GetNames(typeof(UIName)))
for (int i = 0; i < Enum.GetNames(typeof(UIName)).Length; i++)
Copy the code
/ / Jane
using System;
public class ParseTest
{
enum Colors
{
Red = 1,
Green = 2,
Blue = 4,
Yellow = 8
};
public static void Main()
{
foreach (string colorName in Enum.GetNames(typeof(Colors)))
{
Console.WriteLine("{0} = {1}", colorName,
Convert.ToInt32(Enum.Parse(typeof(Colors), colorName)));
}
Console.WriteLine();
Colors myOrange = (Colors)Enum.Parse(typeof(Colors), "Red, Yellow");
Console.WriteLine("{1} {0}", myOrange, Convert.ToInt64(myOrange)); Console.ReadLine(); }}Copy the code