1. Definition of enum
Enum Fabric {Cotton = 1, Silk = 2, Wool = 4, Rayon = 8, Other = 128}
2. Conversion between symbol names and constant valuesFabric 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 );
}
}