C #3.0 Study Notes (5) Enumeration

Source: Internet
Author: User

 

 

1. What is enumeration?

A: enumeration is a type defined by a programmer. It is the same as a class or structure.

Note: 1> like the structure, enumeration is a value type. Therefore, data is directly stored instead of being separated and stored as references and data. Enumeration is stored in the stack.

2> there is only one type of enumeration member: the named integer constant.

3> Each Enumeration type has an underlying Integer type. The default value is int. The compiler assigns the first member 0 and assigns one more value to each subsequent member than the previous one.

2. Examples of enumeration in stack arrangement?

Class Program

 

{

 

Static voidMain (string [] args)

 

{

 

TrafficLight t1 = TrafficLight. Green;

 

TrafficLight t2 = TrafficLight. Yellow;

 

TrafficLight t3 = TrafficLight. Red;

 

Console. WriteLine ("{0}, {1}", t1, (int) t1 );

 

Console. WriteLine ("{0}, {1}", t2, (int) t2 );

 

Console. WriteLine ("{0}, {1}", t3, (int) t3 );

 

Console. ReadKey ();

 

}

 

}

 

Enum TrafficLight

 

{

 

Green,

 

Yellow,

 

Red

 

}

The output result of the program is:

Green, 0

Yellow, 1

Red, 2

3. How does one supplement enumeration?

Because enumeration members are constants, they can be accessed even if they do not have variables of this enumeration type. The enumerated type names are followed by one vertex and member names.

Sample Code:

Class Program

 

{

 

Static voidMain (string [] args)

 

{

 

Console. WriteLine ("{0}", TrafficLight. Green );

 

Console. WriteLine ("{0}", TrafficLight. Yellow );

 

Console. WriteLine ("{0}", TrafficLight. Red );

 

Console. ReadKey ();

 

}

 

}

 

Enum TrafficLight

 

{

 

Green,

 

Yellow,

 

Red

 

}

Program output result:

Green

Yellow

Red

 



The author's wheat forever.

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.