1. Definition of Enum
Enum fabric {cotton = 1, silk = 2, wool = 4, rayon = 8, other = 128}
2. Conversion between symbol names and constant values
Fabric Fab = fabric. cotton;
Int fabnum = (INT) Fab; // convert to a constant value. Mandatory conversion is required. Fabric fabstring = (fabric) 1; // convert a constant value to a symbolic name. If tostring () is used, it is (fabric) 1). tostring (). Note that brackets must exist. String fabtype = Fab. tostring (); // display the symbol name.
String fabval = Fab. tostring ("D"); // display a constant value
3. Obtain all symbolic names (For details, refer to the enum class)
Public Enum myfamily
{
Yangzhiping = 1,
Guanguiqin = 2,
Yanghaoran = 4,
Liwei = 8,
Guanguizhi = 16,
Lisiwen = 32,
Lisihua = 64,
} Foreach (string s in
Enum. getnames (typeof(Myfamily )))
{
Console. writeline (s );
}
4. Process enumeration as a bit flag
According to the two examples below, on the one hand, setting the flag [flags] or [flagsattribute] indicates that the symbol name must be listed. On the other hand, you can use force conversion to list the symbol names, convert a number
Symbol name. Inaccurate. Let's take a look at the example below. Note:
Example 1:Fabric Fab = fabric. Cotton | fabric. Rayon | fabric. Silk;
Console. writeline ("myfabric = {0}", FAB); // output: fabric. Cotton | fabric. Rayon | fabric. Silk;
Example 2:Class flagsattributedemo
{
// Define an Enum without flagsattribute.
Enum singlehue: short
{
Black = 0,
Red = 1,
Green = 2,
Blue = 4
};
// Define an Enum with flagsattribute.
[Flagsattribute]
Enum multihue: short
{
Black = 0,
Red = 1,
Green = 2,
Blue = 4
};
Static void main ()
{
Console. writeline (
"This example of the flagsattribute attribute/N" +
"Generates the following output .");
Console. writeline (
"/Nall possible combinations of values of an/N" +
"Enum without flagsattribute:/N ");
// Display all possible combinations of values.
For (INT val = 0; Val <= 8; Val ++)
Console. writeline ("{0, 3}-{1}", Val, (singlehue) Val). tostring ());
Console. writeline ("/Nall possible combinations of values of an/N" + "Enum with flagsattrieline:/N ");
// Display all possible combinations of values.
// Also display an invalid value.
For (INT val = 0; Val <= 8; Val ++)
Console. writeline ("{0, 3}-{1}", Val, (multihue) Val). tostring ());
}
}
/*
This example of the flagsattribute attribute
Generates the following output.
All possible combinations of values of
Enum without flagsattribute:
0-black
1-red
2-green
3-3
4-blue
5-5
6-6
7-7
8-8
All possible combinations of values of
Enum with flagsattribute:
0-black
1-red
2-green
3-red, green
4-blue
5-red, blue
6-green, blue
7-red, green, blue
8-8
*/
5. enumeration is used as a function parameter. It is often used in combination with a switch. The following example
Public static double getprice (fabric FAB)
{
Switch (FAB)
{
Case fabric. Cotton: Return (3.55 );
Case fabric. Silk: Return (5.65 );
Case fabric. wool: Return (4.05 );
Case fabric. Rayon: Return (3.20 );
Case fabric. Other: Return (2.50 );
Default: Return (0.0 );
}
}
6. The above 3.1 complete examples
// 1. Enum Definition
Public Enum fabric: short
{
Cotton = 1,
Silk = 2,
Wool = 3,
Rayon = 8,
Others = 128
}
// Pass enumeration as a parameter
Public static double getprice (fabric FAB)
{
Switch (FAB)
{
Case fabric. Cotton: Return (3.55 );
Case fabric. Silk: Return (5.65 );
Case fabric. wool: Return (4.05 );
Case fabric. Rayon: Return (3.20 );
Case fabric. Other: Return (2.50 );
Default: Return (0.0 );
}
}
Public static void main ()
{
Fabric Fab = fabric. cotton;
Int fabnum = (INT) Fab;
String fabtype = Fab. tostring ();
String fabval = Fab. tostring ("D ");
Double cost = getprice (FAB );
Console. writeline ("fabnum = {0}/nfabtype = {1}/nfabval = {2}/N", fabnum, fabtype, fabval );
Console. writeline ("cost = {0}", cost );
}
7. Use of Enum class
Enum. isdefinde and enum. parse are often used together to determine whether a value or symbol is an enumeration member and then create an instance. Enum. getname prints the value of a member. enum. getnames prints the value of all members. Note:Typeof. This is important.
Public Enum myfamily
{
Yangzhiping = 1,
Guanguiqin = 2,
Yanghaoran = 4,
Liwei = 8,
Guanguizhi = 16,
Lisiwen = 32,
Lisihua = 64,
}
String S = "yanghaoran ";
If (enum.Isdefined(Typeof(Myfamily), S ))
{
Myfamily F =(Myfamily)Enum.Parse(Typeof (myfamily), S );
Getmyfamily (f );
Console. writeline ("the name is:" + enum.Getname(Typeof(Myfamily), 2 ));
String [] SA = enum.Getnames(Typeof (myfamily ));
Foreach (string SS in SA)
{
Console. writeline (SS );
8. text 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:
Using system. componentmodel;
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:
Using system. reflection;
Using system. componentmodel;
Public static string getenumdesc (Enum E)
{
Fieldinfo enuminfo = E. GetType (). getfield (E. tostring ());
Descriptionattribute [] enumattributes = (descriptionattribute []) enuminfo.
Getcustomattributes (typeof (descriptionattribute), false );
If (enumattributes. length> 0)
{
Return enumattributes [0]. description;
}
Return e. tostring ();
}
Alternatively, you can customize discription attributes :( from: James Geurts 'blog)
Enum direction
{
[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
{
Private string _ text = "";
Public String text
{
Get {return this. _ text ;}
}
Public enumdescriptionattribute (string text)
{
_ Text = text;
}
}