EnumThe keyword is used to declare enumeration, which is a unique type consisting of a group of named constants called the enumerated number list. Each Enumeration type has a base type, which can be any integer except char. That is:
(Byte, sbyte, short, ushort, Int, uint, long, And ulong)
The concept of emumeration was available in the C era, but it has never been used before.
Basic expression, changing the default value and default type
The default value of enumeration is int starting from 0, as follows:
Enum Direction
{
Up,
Right
Down,
Left,
};
In this case, up = 0, down = 1...
Change default value:
Enum Direction
{
Up = 1,
Right = 2,
Down = 3,
Left = 4,
};
Change the type (only byte, sbyte, short, ushort, Int, uint, long, ulong can be changed ):
Enum Direction:Long
{
Up = 1111111111,
Go down = 1111111112,
Left = 1, 1111111113,
Right = 1111111114.
};
Value of the enumeration variable
Cast (forced type conversion) before assigning values ):
LongDirect = (Long)Direction. Up;
Description of the enumeration variable
If you want enumeration to return a meaningful string so that you can know what it represents, you can define it as follows:
UsingSystem. componentmodel;// Add the reference first
Enum Direction
{
[ Description ( "This means facing to up (negtive y )" )]
Up = 1,
[Description("This means facing to right (positive X )")]
Right = 2,
[ Description ( "This means facing to down (positive y )" )]
Down = 3,
[ Description ( "This means facing to left (negtive X )" )]
Left = 4
};
Use the following method to obtain the text description:
UsingSystem. reflection;
UsingSystem. componentmodel;
Public Static String Getenumdesc ( Direction E)
{
Fieldinfo Enuminfo = E. GetType (). getfield (E. tostring ());
Descriptionattribute [] Enumattributes = ( Descriptionattribute []) Enuminfo.
Getcustomattributes ( Typeof ( Descriptionattribute ), False );
If (Enumattributes. length> 0)
{
ReturnEnumattributes [0]. description;
}
ReturnE. tostring ();
}
Alternatively, you can customize discription attributes
Enum direction
{< br> [ enumdescription (" Rover is facing to up (negtive y) ")]
up = 1,
[ enumdescription (" Rover is facing to down (positive y) ")]
down = 2,
[ enumdescription (" Rover is facing to right (positive X) ")]
right = 3,
[ enumdescription (" Rover is facing to left (negtive X) ")]
left = 4
};
Attributeusage(Attributetargets. Field)]
Public Class Enumdescriptionattribute : attribute
{< br> private string _ text = " ;
Public string text
{< br> Get { return This . _ text ;}< BR >}< br> Public enumdescriptionattribute ( string text)
{< br> _ text = text;
}
}
Although now ,. in net, the tostring operation on Enum will output the enum name. However, in many cases, we want to display it in different languages, or, the string we want to display contains invalid characters (such as spaces, which is a common requirement for explicit display, it helps us to display the string defined by Enum, but unfortunately, although any Enum is from system. enum is derived. However, we cannot rewrite its tostring function. Therefore, we need a helper class to help its output:
Public Class Enumstringhelper
{
Public Static String Tostring (Object O)
{
Type T = O. GetType ();
String S = O. tostring ();
Enumdescriptionattribute [] OS = ( Enumdescriptionattribute []) T. getfield (s). getcustomattributes ( Typeof ( Enumdescriptionattribute ),False );
If (OS! = Null & Amp; OS. Length = 1)
{
Return OS [0]. text;
}
Return S;
}
}
Usage, like this:
Static VoidMain (String[] ARGs)
{
Direction mydirection = direction. down;
StringS = enumstringhelper. tostring (mydirection );
Console. writeline ("{0 }", S );
Console. readkey ();
}