Function of Enumeration type in C #

Source: Internet
Author: User

C # programming language, as a powerful language, plays a very important role for developers. However, there are many application methods that we need to constantly explore and learn from them to reach the level of proficiency. Next, we will introduce in detail the concepts related to the C # Enumeration type.

The main role is a Convention: to give a common example, you want to represent week 1-7, you can use int1-7, but when you take it as a parameter, sometimes you will consider the situation of passing 0, 8. in addition, corresponding comments and documents are also required in numbers. at this time, you define an enumeration. The name is literally Monday, Tuesday ,.... it is intuitive and controllable.

When defining the value of an element in the enumeration type, the default value of the first element starts from 0, followed by 1

Int type data is a 32-bit signed integer data, and does not distinguish between positive and negative

So public enum Test

The definitions such as {color1 =-1, color2, color3} are correct.

1. Simple examples of enum Enumeration

When writing a program, we often need to associate a set of optional alternative attributes for an object. for example, student scores include A, B, C, D, sunny, cloudy, and rainy. More often, opening a file may have three states: input, output, and append. A typical practice is to define three constants, namely:

 
 
  1. const int input = 1;
  2. const int output = 2;
  3. const int append = 3;

Then, call the following functions:

 
 
  1. bool open_file(string file_name, int open_mode);

For example,

 
 
  1. open_file("Phenix_and_the_Crane", append);

This method is relatively simple, but there are many shortcomings. The main point is that the value range of the 2nd parameter passed to the open_file function cannot be limited, as long as the int type value is passed, it is legal. (Of course, in this case, the response is to judge the value of the second parameter within the open_file function, which is only handled within the range of 1, 2, and 3 .)

Enumeration can alleviate this embarrassment to some extent (Note 1). It not only implements functions similar to the three constants defined previously, these three values can also be combined into a unique group. For example:

 
 
  1. enum open_modes {input = 1, output, append};

Open_modes is defined as the enumeration type of the C # enumeration type. Each named enumeration is a unique type and is a type specifier. For example, we can re-write an open_file function:

 
 
  1. bool open_file(string file_name, open_modes om);

In open_modes enumeration, input, output, and append are called enumeration subenumerator, which limit the value range of objects defined by open_modes. At this time, the call to the open_file function is exactly the same as the previous method:

 
 
  1. open_file("Phenix_and_the_Crane", append);

However, if the second parameter passed to open_file is not of the open_modes Enumeration type value (note 1), the compiler will identify the error. Even if the parameter value is equivalent to input, output, append,
The same is true! For example:

 
 
  1. open_file("Phenix_and_the_Crane", 1);

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.