An enumeration is a specified constant whose underlying type can be any integral type except Char. If you do not explicitly declare an underlying type, use Int32.
Defined
The default cardinality starts at 0, and you can specify a value.
enumDays {Saturday,//The default cardinality starts with OSunday,//1Monday, Tuesday, Wednesday, Thursday, Friday};enumColors {Red=1,//Specify a default cardinality of 1Green,//2Blue =4, Yellow=8};
public enum Noticetype
{
Notice = ' A ',
Labrule = ' H ',
hotinformation = ' N ',
Column = ' C ',
all = ' 1 ',
Null = ' 0 '
};
Use
The Enum provides some useful static methods:
- Methods to compare instances of an enumeration class
- method to convert the value of an instance to its string representation
- method to convert the string representation of a number to an instance of this class
- Creates a method that specifies an instance of an enumeration and a value.
Colors mycolors = colors.red;//New Enumeration ObjectNoticetype noticetype = Noticetype.column;//New Enumeration ObjectstringStrcolor = Mycolors.tostring ();//Enum-->string,strcolor value is "Red"stringStrcolor = Enum.getname (typeof(Colors),2));//Enum-->string,strcolor value is "Green"stringStrcolor = Enum.getname (typeof(Colors), colors.blue));//Enum-->string,strcolor value is "Blue"string[] Strdayarray = Enum.getnames (typeof(days));//enum-->string[], used to traverseDays DS= (days) enum.parse (typeof(days),"Monday");//String-->enumColors MC = (Colors) enum.parse (typeof(Colors),"Red");//String-->enumNoticetype nt= (noticetype) Enum.parse (typeof(Noticetype),"Notice");//String--enumintIntcolor= (int) colors.red;//enum--> base class with a Intcolor value of 1byteBytecolor= (byte) Colors.green;//enum--> base classCharDD = (Char) Noticetype;//enum--> base class, DD value is ' C 'Colors Color= (Colors)2;//base class-->enumColors color = (Colors) enum.toobject (typeof(Colors),2);//base class-->enumNoticetype Noticetype = (noticetype)'A';//base class-->enumNoticetype Noticetype = (noticetype) char.parse ("A");//base class-->enum another wayBOOLisdefined = enum.isdefined (typeof(Colors),2));//determines whether an enumeration with a base class value of 2 existsBOOLisdefined = System.Enum.IsDefined (typeof(days),"Monday")//determines whether an enumeration with the name value of "Monday" existsColors mycolors= Colors.red | Colors.blue | Colors.yellow;//bit orColors mycolors = colors.red & Colors.blue & Colors.yellow;//bit and
Resources
Http://www.cnblogs.com/an-wl/archive/2011/04/14/2015815.html
Http://www.cnblogs.com/pato/archive/2011/08/15/2139705.html
C # Enumeration types