Enum type (enum type) in C #

Source: Internet
Author: User

Ylbtech enum type (enum type) in the original C #

A conceptual enumeration type (enum type) is a unique value type with a set of named constants. In the following example:
Enum color{     Red,    Green,    Blue}

Declares an enumeration type named Color that has three members: Red, Green, and Blue.

How is enumeration specifically declared? An enumeration declaration is used to declare a new enumeration type. An enumeration declaration begins with the keyword enum and then defines the name, accessibility, underlying type, and members of the enumeration type. Specific format:

Modifiers (new, public, protected, internal, private) enum enum type name: integer type

{     enum-member-declarations,    enum-member-Declaration}   

Test example enumeration types are typically used to list unique elements, such as the day of the week, the name of a country/region, and so on. The following example code declares and uses an enumeration type named Color that has three constant values, Red, Green, and Blue.
UsingSystem;Namespaceconsoleapplication1{EnumColor {Red, Green, Blue}ClassEnumtypeexample {StaticvoidPrintcolor (color color) {Switch(color) {CaseColor.Red:Console.WriteLine ("Red");Break;CaseColor.Green:Console.WriteLine ("Green");Break; case Color.Blue:Console.WriteLine (" blue "break; default: Console.WriteLine (  "unknown color" breakstatic void Main (string< Span style= "COLOR: #000000" >[] args) {color c = color.red; Printcolor (c); Printcolor (Color.Blue); } }} 

Execation Result

Redblue Please press any key to continue ...

Assign value

The associated value of an enumeration, or implicitly, or explicitly, is assigned. If an enumeration member has a constant-expression initializer in its declaration, the value of the constant expression (which is implicitly converted to the underlying type of the enumeration) is the associated value of the enumeration member. If the declaration of an enumeration member does not have an initializer, its associated value is implicitly set according to the following rule:

If an enumeration member is the first enumeration member declared in an enumeration type, its associated value is zero. Otherwise, the associated value of an enumeration member is obtained by adding 1 to the association value of the previous enumeration member (in text order). The added value must be within the range of values that the underlying type can represent, otherwise a compile-time error occurs. For an enumeration type member association value assignment case, refer to the following example:

UsingSystem;Namespacetest{PublicEnum Day:UInt//If you do not set the data type, the default is int {mon=1,tue=2,wed=3,thu=4,fri=5, Sat,sun}ClassEnumtype {PublicStaticvoid Main (String[] args) {Console.WriteLine (Chooseday (Day.sun)); Console.WriteLine (Chooseday (Day.mon)); }StaticStringChooseday (Day D) {string tmp =String. Empty;Switch(d) {CaseDay.Mon:tmp =String. Format ("You choose {0} Days of the week (that is, Monday), work",(UInt) d);Break;CaseDay.Tue:tmp =String. Format ("You choose {0} Days of the week (that is, Tuesday), work", (UInt) d);Break;CaseDay.Wed:tmp =String. Format ("You choose {0} Days of the week (that is, Wednesday), work", (UInt) d);Break;CaseDay.Thu:tmp =String. Format ("You choose {0} Days of the week (that is, Thursday), work", (UInt) d);Break;CaseDay.Fri:tmp =String. Format ("You choose {0} Days of the week (that is, Friday), work", (UInt) d);Break;CaseDay.Sat:tmp =String. Format ("You choose the {0} Day of the week (that is, Saturday) and Rest", (UInt) d); break; case Day.Sun:tmp = string. Format ( " you choose the {0} Day of the week (that is, Sunday), rest uint) d); break; default"  illegal  "; break;} return tmp;}}        

Execation Result

You choose the 7th Day of the week (that is, Sunday), rest you choose the 1th Day of the week (that is, Monday), work please press any key to continue ...

Enum type (enum type) in C #

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.