C # Learning diary 11 --- data type enumeration (enum) Type

Source: Internet
Author: User

C # Learning diary 11 --- data type enumeration (enum) Type

All the values I have introduced earlier are value types. If so many values are written, I will summarize the following values. Simply put, this type of variable directly stores the included values. Here we compare it with the string in C language, char ch [6] = HC666; the teacher said that it is actually to use ch to reference the "HC666" address and access data through the access address. The same is true for string. Therefore, String in C # is the reference type rather than the value type .. The struct type is not like this. The variable storage content is changed by direct value assignment, so it is a value type.

Enumeration (enum) Type of Value Type:

Enumeration (enum): it is actually a set of logically inseparable integers that provide easy-to-remember symbols. For example, on Sunday, I bought a movie ticket to take a movie. The seat number is good. --- 66. I found the location by phone number, A good location-2nd rows and 5th columns, so I had a good time watching the movie. In fact, on the 66 th, 2nd rows and 5th columns are reflected in my projection room. This is called an enumeration.

Declare enum type variables:

Enum variable name

{

Identifier list

};

 

Note:

In the identifier list, elements and elements are separated by commas;

 

 

Each symbol in the enumeration list represents an integer, and an integer greater than the previous symbol. By default, the first enumeration symbol represents 0, which can also be modified:

Using System; using System. collections. generic; using System. linq; using System. text; namespace Test {class Program {// defines the enum type to increase from 0 weekday {Sun, Mon, tue, Wed, thu, Fri, sat // to change the default value (Sun = 2, Mon, tue, Wed, thu, Fri, Sat), increase by 2 + 1 }; static void Main (string [] args) {weekday wek; Console. writeLine (input 0 ~ 6 digits:); int I = int. parse (Console. readLine (); wek = (weekday) I; // force type conversion to convert int type to weekday type Console. writeLine ({0} is {1}, I, wek );}}}


I enter 0:

 

For the forced type conversion in the code, I will write it out in the implicit conversion and display conversion following it. I will not introduce it now.

Comparison between the structure (struct) type and enumeration (enum) type:

I have a classmate who is always confused about the Struct type and the enum type. Sometimes this error code is written:

Enum wek

{

Int a, B, c;

String d, e, f;

}

 

 

To avoid the above errors, let's talk about the differences between them: when talking about the struct type, I have mentioned that the data types in it can be multiple different types, in other words, the Struct type is a group of new types composed of different types of data. The value of the structure type variable is composed of the values of each member. For example:

Struct wek

{

Public int a = 1;

Public int B = 2;

}

Define a variable wek w. In this case, w can be used to indicate that the value of a is 1 (w. a) or 2 (w. B)

The enumeration (enum) type is different. variables of the enumeration type can only take the values of one element in the enumeration list at a certain time, for example: in the enum weekday program I wrote earlier, wek can only represent one value at a time point. It cannot represent Sun or Mon at the same time point,

Wek = (weekday) 2; in this case, wek can only represent Tue and cannot represent others.

 

 

 

 

 

The value type is introduced, and the reference type will become more and more difficult, and the update will be slower (You have to check the information in some places). Thank you for reading this article, your comments and comments are my motivation to improve myself. Thank you!

 

 

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.