13. C # Basic Grooming (enumeration)

Source: Internet
Author: User
Tags try catch

Enumeration

1. Concept and role

(1) For storing constants, assigning values only at the time of definition (preventing malicious tampering during programming and preventing different assignments of the same thing-unifying)

(2) A defined enumeration type needs to contain all possible values of that type

(3) Methods, classes, and interiors can be defined, generally and classes are defined at the same level, so that all classes can be enumerated with this

2. Definition:

Enum type name {value 1, value 2, value 3 ... Value N}

(the default index starts at 0 and the value cannot be of type int)

enum Gender// Define an enumeration type, only "male", "female" two value {    male,// when defining an enumeration, do not need double quotes     Female} 

3. How to use:

Switch (Sex) {  case  Gender male:          Console.WriteLine (" male ") ;            Break ;    Case Gender. Female:          Console.WriteLine (" female ");            Break ;}

4. Enum types can be cast to type int---index

= Gender. Male; Console.WriteLine ((int) sex); // Output 0--Index, * * if you write "= 2" on the back of the male, it will output 2, the female will output 3

5. Convert a string to an enumeration type: (user input-judged by try Catch)

(Gender) (Enum.parse (typeof (Gender), "string to convert")//gender write its own defined name

13. C # Basic Grooming (enumeration)

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.