What is enumeration?
Enumeration is a set of named integer constants. Enumerated type is to use the enum keyword declared. Enumeration is a value type. In other words, the enumeration contains its own value and can not inherit or transfer inheritance.
Enumeration definitions :
public enum Direction { [Description("东")] East, [Description("南")] South, [Description("西")] West, [Description("北")] North }
The default value is zero-based enumeration int, int next enumerated value will automatically add a tired enumeration value. For example: East = 1, then the South = 2, West = 3, and so on. . . Or East = 4, then the South = 5, West = 7, North = 8. . .
Gets int value enumeration:
var eastNumber = (int)Direction.East; // eastNumber = 0;
Gets an enumeration of strings:
var eastString = Direction.East.ToString(); // eastString= "East";
int turn enumeration:
var east = (Direction)0; // east = Direction.East
string turn enumeration:
String strEnum = const "East"; // ignoreCase: to true / false (whether or not ignore case .true: Ignore case (default), false: do not ignore) var East that Enum.Parse = (typeof (Direction), strEnum, ignoreCase : to true); // Direction.East = East;
For a description (Description) enumeration:
/// <Summary> /// Gets an enumeration method described /// </ Summary> /// <param name = "enumValue"> </ param> /// <Returns> </ Returns> public static String GetEnumDescription (the Enum enumValue) { String value = enumValue.ToString (); the FieldInfo Field enumValue.GetType = () GetField (value);. Object [] = field.GetCustomAttributes OBJS (typeof (the DescriptionAttribute), to false); // Get the description attribute if (objs.Length == 0) // if there is no description attribute, the name of the direct return return value; the DescriptionAttribute DescriptionAttribute = (the DescriptionAttribute) OBJS [0]; return descriptionAttribute.Description; }
// Call Gets an enumeration of the methods described var = GetEnumDescription the Description (Direction.East); // the Description = "East";
Enumeration loop:
foreach (Direction item in Enum.GetValues(typeof(Direction))) { // item is East/South/West/North }