Enumeration in c #

Source: Internet
Author: User

1. enumeration concept: enumeration is a user-defined Integer type. When declaring an enumeration, you must specify a group of acceptable values that can be included in the enumeration instance, you can also specify a name that is easy to remember. If you try to assign an unacceptable value to an enumeration instance somewhere in the code, compilation will fail.

2. Advantages of enumeration:

(1) Enumeration makes the code easy to maintain and helps to specify valid and expected values for the variable.

(2) Enumeration makes the code clearer. The description name can be used to represent an integer, rather than a fuzzy or variable number.

(3) Enumeration makes the code easy to type.

Example:

// Define an enumeration
Public enum TimeOfDay {
Morning = 0,
Afternoon = 1,
Evening = 2
}

// Main function entry
Static void Main (string [] args)
{
Main ();
Console. ReadLine ();
}
// Call the implementation method
Public static int main (){
WriteGreeting (TimeOfDay. Morning );
Return 0;
}


// Implementation Method

Public static void WriteGreeting (TimeOfDay timeofday) {// pass enumeration as a parameter
Switch (timeofday ){
Case TimeOfDay. Morning:
Console. WriteLine ("Good morning! ");
Break;
Case TimeOfDay. Afternoon:
Console. WriteLine ("Good Afternoon! ");
Break;
Case TimeOfDay. Evening:
Console. WriteLine ("Good Evening! ");
Break;
}
}

Console output result: Good morning!

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.