Introduction to data types in C # (enumeration) The definition of an enumeration
According to the definition given on MSDN, an enumeration is a specified set of constant numbers whose underlying type can be any integral type except char. If you do not explicitly declare an underlying type, use Int32. an enum is the base class for all enumerations in the. NET Framework. The underlying type can be byte, sbyte, short, ushort, int, unit, long, ulong. By default, the value of the first enumerator is 0, and subsequent enumerators are added in turn by 1.
char. " > int32 is used. " > enum is the base class for all enumerations in the. NET Framework. >
Enumerations can be declared under a namespace and at a similar level, or within a class. The declaration syntax is as follows:
[Access modifiers] enum <identifier> [: Enum_base]
{
Enum body
}
Access modifiers: Define access modifiers, only public and internal, default internal
Identifier: Defines the name of the enumeration
Enum_base: Defines the type of an enumerated integer, which by default is int (any integer type other than char can be defined, as described in the definition section of the above enumeration)
namespace enumdemo{ enum color:byte // {Red, Orange, Yellow, Green, Cyan, Blue, Purple, // You can omit the comma }; // class program { static void Main (string [] args) {}} }
enum Color:byte // base type is byte { Red=2, // You can redefine the default value with the base number, the red base number definition starts at 2, the orange base number is 3, the subsequent constant value, and so on,
//But it is recommended that the underlying data definition contain a default value, otherwise the instantiation of the enumeration may cause problems Orange, Yellow, Green, Cyan, Blue, Purple, };
instantiating enum types
There are three ways to instantiate an enumeration value
1, direct assignment as enumeration constant
2, conversion assignment by enumeration value
3, instantiate the enumeration value by constructor function
classProgram {Static voidMain (string[] args) {Color MyColor= Color.yellow;//Assignment Enumeration ConstantsColor MyColor1 = (color)4;//assign an enumeration value and then convert to an enumeration constantColor MyColor2 =NewColor ();//The initialization of the constructor, the enumeration value of the MyColor2 assignment is 0, but the minimum value of the color enumeration is 2, which is an illegal valueConsole.WriteLine ("my color is {0}, color value is {0:d}", MyColor); Console.WriteLine (MyColor1); Console.WriteLine ("my color is {0},color value is {1}", (Color) mycolor2,mycolor2); Console.read (); } }Conversion of enumeration types
The System.Enum type is an abstract base class for all enum types (it is a unique type that differs from the underlying type of the enumeration type), and the members inherited from System.Enum are available in any enum type. There are boxing conversions from any enumerated type to System.Enum, and there is an unboxing conversion from System.Enum to any enumerated type. System.Enum itself is not an enumeration type. Instead, it is a class type, and all enum types are derived from it. The type System.Enum derives from the type System.ValueType and the latter derives from the type object. At run time, the value of type System.Enum can be null or a reference to the bin value of any enumerated type.
classProgram {Static voidMain (string[] args) {Color Color1= (Color)6;//Convert an integer to an enumeration constant intInt1 = (int) Color.green;//converting enumerated constants to integersConsole.WriteLine (INT1); string[] format = {"G","F","X","D" }; foreach(stringFinchformat) { stringColorstr = Color1. ToString (f);//formatted outputConsole.WriteLine (COLORSTR); } Console.WriteLine ("Color Format is: {0}", Enum.format (typeof(Color), (byte)7,"G"));//to convert an enumeration value to a string by using the Format methodConsole.WriteLine ("Color Constant is: {0}", Enum.getname (typeof(Color), (byte)8));//converting an enumeration value to string,8 by the GetName method is the underlying type that the int type needs to be converted to color, byte foreach(stringIteminchEnum.getnames (typeof(Color)))//converting an enumeration to a string array by using the GetName methodConsole.WriteLine ("Color Name Constant is: {0}", item); foreach(Color IteminchEnum.getvalues (typeof(Color)))//convert to a color constant by the GetValues methodConsole.WriteLine ("Color value is {0}", item); if(Enum.isdefined (typeof(Color), (byte)4)) Console.WriteLine ("The value 4 ' s constant is {0}", Enum.parse (typeof(Color),"4")); ElseConsole.WriteLine ("The 4 value is not define"); Color Color2= (Color) enum.toobject (typeof(Color),4);//converting an integer value to an enumeration constant by means of the Toobject methodConsole.WriteLine (COLOR2); Color Color3; if(Enum.tryparse ("2", outCOLOR3))//Convert a string to an enumeration constant by TryParseConsole.WriteLine ("The value 2 is Defined: {0}", Color3); ElseConsole.WriteLine ("The value 2 is not Defined"); if(Enum.tryparse ("BLACK", outCOLOR3))//Convert a string to an enumeration constant by TryParseConsole.WriteLine ("The CONSTANT BLACK is Defined: {0}", Color3); ElseConsole.WriteLine ("The CONSTANT BLACK is not Defined"); Console.read (); } }Bit arithmetic
Add the [Flags] attribute to the declaration of the enumeration
classProgram {[Flags]enumfamilypets {None=0x0,//definition of 16 binary 0Dog =0x01,//definition of 16 binary 1Cat =0x02, Bird=0x04, Duck=0x08, }; Static voidMain (string[] args) {familypets FP= Familypets.dog |Familypets.cat; Console.WriteLine ("Pets: {0:g} ({0:d})", FP);//Display Results Dog,cat (3), 16 binary 1 and 2 bitwise OR operation result is 3 if(FP. Hasflag (Familypets.cat))//determine if FP defines bit field catConsole.WriteLine ("there are pet cats. "); ElseConsole.WriteLine ("No pet cat. "); if(fp & familypets.dog) = = Familypets.dog)//Bitwise AND operation of FP and dog bit fieldsConsole.WriteLine ("have pet dog. "); ElseConsole.WriteLine ("No pet dog. "); if(fp = = Familypets.none)//The result of the FP bit operation is 3, not 0Console.WriteLine ("There are no pets in the family. "); ElseConsole.WriteLine ("There are pets in the family. "); Console.read (); } }
Introduction to data types in C # (enumeration)