Enumeration type (C # programming guide)

Source: Internet
Author: User
Enumeration type (C # programming guide)

Posted on ottox read (681) Comments (0 ){
Open_link ('HTTP: // www.cnblogs.com/ottox/admin/editposts.aspx? Postid = 1402386 ')
} "Rel =" nofollow "href =" http://writeblog.csdn.net/# "> edit {
Addtowz( 1402386); Return false;
} "Href =" http://writeblog.csdn.net/# "> collections of enumeration types (C # programming guide)

 

Updated: February 1, November 2007

Enumeration type (also called enumeration) provides an effective method to define a group of named Integer constants that can be assigned to variables. For example, if you must define a variable, the value of this variable indicates one day in a week. This variable can only store seven meaningful values. To define these values, you can use the enumeration type. The enumeration type is declared using the enum keyword.

C # Enum days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
Enum months: byte {Jan, Feb, MAR, APR, May, Jun, Jul, Aug, SEP, Oct, Nov, Dec };

By default, the basic type of each element in the enumeration is int. You can use a colon to specify another Integer type, as shown in the preceding example. The following are the benefits of enumeration instead of numerical type:

  • Specify the values of the variables for the client code.

  • In Visual Studio, intelliisense lists the defined values.

If you do not specify a value for the element in the list of enumerative numbers, their values will automatically increase in increments of 1. In the previous example, the value of days. Sunday is 0, the value of days. Monday is 1, and so on. When creating a new days object, if you do not explicitly assign a value to it, it will have the default value days. Sunday (0 ). When creating an enumeration, select the most reasonable default value and assign it a zero value. This means that as long as the enumeration is not explicitly assigned a value when it is created, all the enumerated values will have the default value.

If the meetingday type of the variable is days, only a value defined by days can be assigned to it (no explicit forced conversion is required ). If the meeting date is changed, you can assign the new value in days to meetingday:

C #
Days meetingDay = Days.Monday;//...meetingDay = Days.Friday;
Note:

You can assign any integer to meetingday. For example, the code line meetingday = (days) 42 will not produce errors. However, this should not be done either, because the default convention is that enumeration variables only hold one of the enumerated values. An error may occur if any value is assigned to an enumerated variable.

You can assign any value to an element in the enumerated number list, or use the calculated value:

C #
enum MachineState{PowerOff = 0,Running = 5,Sleeping = 10,Hibernating = Sleeping + 5}
Enumeration type as a bit flag

You can use the enumeration type definition bit flag so that instances of this enumeration type can store any combination of values defined in the enumeration series table. (Of course, some combinations may be meaningless or not allowed in your program code .)

The method to create a bit flag enumeration is to apply the system...:. flagsattribute attribute and define some values as appropriate, so that you can perform and, or, not, and XOR bitwise operations on these values. The in-place flag enumeration contains a named constant whose value is zero (indicating "no flag is set. If the zero value does not indicate "no flag is set", do not specify a zero value for the flag.

In the following example, another version of days enumeration is defined, namely days2. Days2 has the flags attribute, and each of its values is several power of 2, and the index increases sequentially. In this way, you can create days2 variables with values of days2.tuesday and days2.thursday.

C #
[Flags]enum Days2{None = 0x0,Sunday = 0x1,Monday = 0x2,Tuesday = 0x4,Wednesday = 0x8,Thursday = 0x10,Friday = 0x20,Saturday = 0x40}class MyClass{Days2 meetingDays = Days2.Tuesday | Days2.Thursday;}

To set a flag on an enumeration, use the logical or operator, as shown in the following example:

C #
// Initialize with two flags using bitwise OR.meetingDays = Days2.Tuesday | Days2.Thursday;// Set an additional flag using bitwise OR.meetingDays = meetingDays | Days2.Friday;Console.WriteLine("Meeting days are {0}", meetingDays);// Output: Meeting days are Tuesday, Thursday, Friday// Remove a flag using bitwise XOR.meetingDays = meetingDays ^ Days2.Tuesday;Console.WriteLine("Meeting days are {0}", meetingDays);// Output: Meeting days are Thursday, Friday

To determine whether a specific flag is set, use the logical and operation, as shown in the following example:

C #
// Test value of flags using bitwise AND.bool test = (meetingDays & Days2.Thursday) == Days2.Thursday;Console.WriteLine("Thursday {0} a meeting day.", test == true ? "is" : "is not");// Output: Thursday is a meeting day.

For more information about the considerations when using the system...:. flagsattribute attribute to define the enumeration type, see system...:. enum.

Use the system. Enum method to discover and operate on enumeration values

All enumeration types are system...:. Enum instances. You cannot derive a new class from system...:. Enum, but you can use it to find information about the values in the enumeration instance and operate on these values.

C #
string s = Enum.GetName(typeof(Days), 4);Console.WriteLine(s);Console.WriteLine("The values of the Days Enum are:");foreach (int i in Enum.GetValues(typeof(Days)))Console.WriteLine(i);Console.WriteLine("The names of the Days Enum are:");foreach (string str in Enum.GetNames(typeof(Days)))Console.WriteLine(str);

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.