C # Enumeration types

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.