C # Enumeration type,

Source: Internet
Author: User

C # Enumeration type,

The enumerated type is composed of a group of Integer constants and supports the following basic types: byte, sbyte, short, ushort, int, unit, long, And ulong. Default Enumeration type: int.

1. one of the advantages of enumeration: in terms of understanding, you can use frequently used values as enumeration types to easily predict the meaning of the Code, such as turning the light status On = 1, Off = 0;

Ii. Make the code more standard to prevent accidental calls;

2. The enumerated type is inherited from the Enum class, which is derived from the ValueType class. Therefore, the enumerated type is the value type.

3. Declare the enumeration type:

enum yiqiok{a,b,c}

In the preceding statement, the values of a, B, and c are 0, 1, and 2.

You can also customize the constant values in the enumeration:

enum yiqiok{a=5,b,c}

The values of a, B, and c are 5, 6, and 7 in sequence.

The unspecified values in enumeration always add one to the previous one. The default value starts from 0.

Declare the basic Enumeration type as non-int:

enum yiqiok :long{a,b,c}

4. Obtain the enumerated Value List:

Class Program {enum yiqiok {a = 9, B = 3, c = 8}; public static void Main () {var values = Enum. getValues (typeof (yiqiok); // use var to declare foreach (int v in values) {Console. write (v + "\ t ");}}}

Result:

5. Obtain the names of the enumerated members:

Enum yiqiok {a = 9, B = 3, c = 8}; public static void Main () {Console. writeLine ("enumeration yiqiok constant 8 name: {0}", Enum. getName (typeof (yiqiok), yiqiok. c); // output the string [] names = Enum. getNames (typeof (yiqiok); foreach (string name in names) // output Member names {Console. write (name + "\ t ");}}

Result:

 

Related Article

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.