Home » ASP.NET » [计] enumeration

[计] enumeration

Class Program

{

Static void Main (string[] args)

{

* 1) it allows the use of descriptive names represent integral values of.

2) the enumeration itself can have modifiers, but the members of the enumeration are always public and cannot have access modifiers. The modifier of the enumeration itself can only use public and internal.

3) enumeration is the value type.

4) enumerations are implicitly sealed and are not allowed to be children of the base class,.

5) the members of enumerated types are static.

6) each enumerated member has an associated constant value.

7) enumeration members cannot be the same, but enumerated values can be the same as.

8) enumerate the comma of the last member and the semicolon after the braces. You can omit the.

*/





Console.WriteLine (week.Monday);

Console.WriteLine ((int) week.Monday);

Console.WriteLine ((int) week.Tuesday);

Console.WriteLine ((int) week.Wednesday);

Console.WriteLine ((int) week.Thursday);

Console.WriteLine ((int) week.Sunday);





Console.WriteLine (Enum.GetName (typeof (Man), 1)); / / get the 2 value corresponding to the enumerator

String[] array1 = Enum.GetNames (typeof (Man)); / / the enumerated type into a string array

Console.WriteLine (array1[1]);

Array array2 = Enum.GetValues (typeof (Man));

Console.WriteLine (array2.GetValue (1));





Console.WriteLine ("------Enume.GetValues gets the array of enumerated items (base class) -"); "
"
Array ary = Enum.GetValues (typeof (Color)); //array is the base class of an array and cannot instantiate

Foreach (int i in ary) / / digital
list item corresponding enumeration
{

Console.WriteLine (i.ToString ());

}

Console.WriteLine ("------------------------------------------------");

Foreach (Color C in ary) / / list enumeration and the value of

{

Console.WriteLine (c.ToString ());

}





Console.WriteLine ("------ary.GetValue gets the enumeration entry");

//Enumes.GetValues and ary.GetValue get the enumerated item

For (int, I = 0, I, < ary.Length, i++)

{

Console.WriteLine (ary.GetValue) (I) (.ToString); / / by (Color) ary.GetValue (I) obtained
enumeration items
}

Console.WriteLine ("------Enume.GetName gets the string array of enumerated items").

//Enume.GetNames and Enum.Parse get the enumerated item

String[] strary = Enum.GetNames (typeof (Color));

Foreach (string, s, in, strary)

{

Console.WriteLine (s);

}

Console.WriteLine ("------Enum.Parse gets the enumeration entry");

For (int, I = 0, I, < strary.Length, i++)

{

Console.WriteLine (Enum.Parse (typeof (Color), strary[i]),.ToString ());

}





Console.ReadKey ();

}

Enum sex: byte /
shows that the underlying data type specified enumeration
{

Male,

Female, here you can omit the comma / /

}; / / the semicolon can be omitted

Enum week

{

Monday /
starts from here does not assign 0
Tuesday,

Wednesday = 3,

Thursday, here is not a / / assignment would be extended value for 4





Sunday = 0,

Everyday = 1 / member values can be set to the same, but not a member of

}





Enum Man

{

Liu Bei = 1,

Guan Yu = 2,

Zhang = 3

}





Public enum Color

{

Red = 0, orange = 1, yellow = 2, green = 4, blue = 5, cyan = 6, and purple = 7

}





}

copyright notice: This article is original article for blogger. Without permission from blogger,.

must not be reprinted

Latest