C # enum enum

Source: Internet
Author: User

Enumerations are a type of C # programming language that are of the same level as classes, structs, sealed classes, distribution classes, delegates, events, and abstractions.

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

Enumerations are value types, implicitly inherited from System.Enum, and cannot be modified manually.

Enumeration members cannot be the same, but the value of an enumeration can be the same, by default, the first variable in the enumeration is assigned a value of 0, the value of the other variable is incremented in the order defined (0,12,3 ...), if some member of the enum explicitly defines the value, and the part does not, Then a member that does not define a value is incremented by the value of the previous member.

Common methods:

Enumeration conversions:

Enum-->string:

1. Use the Object.ToString () method: The value of Colors.Green.ToString () is a "Green" string;

2. The static method of using the Enum GetName and GetNames: for example: Enum.getname (typeof (Colors), 3)) and Enum.getname (typeof (Colors), colors.blue) are "Blue"

Enum-->int:

Because the base type of the enumeration is an integral type other than Char, you can cast it.

For example: (int) colors.red, (byte) colors.green

String-->enum:

public static Object Parse (Type enumtype,string value)

For example: (Colors) Enum.parse (typeof (Colors), "Red")

Int-->enum:

You can cast an integral type to an enumeration type.

For example: Colors color = (Colors) 2, then color is Colors.blue

method to determine whether an integral type is defined in an enumeration: enum.isdefined

public static bool IsDefined (Type enumtype,Object value)

Example: enum.isdefined (typeof (Colors), 3))

Enumeration traversal:

foreach (intinenum.getvalues(typeof(ItemCode))) {
" " + Good);}

C # enum enum

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.