C # enum type

Source: Internet
Author: User

Reference:

Http://www.cnblogs.com/an-wl/archive/2011/04/14/2015815.html

Convention first on MSDN:

https://msdn.microsoft.com/zh-cn/library/cc138362 (v=vs.110). aspx

An enumeration type (also known as an enumeration) provides an efficient way to define a set of named integer constants that can be assigned to variables. For example, suppose you must define a variable with a value that represents the day of the week. This variable can store only seven meaningful values. To define these values, you can use an enumeration type. Enumeration types are declared using the enum keyword.

1 enum Days{sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}; 2 enum byte {Jan, Feb, Mar, APR, May, June, Jul, Mar, Sep, Oct, Nov, Dec}

By default, the underlying type of each element in the enumeration is int. You can use a colon to specify another integer value type, as shown in the previous example.

1Days today =Days.monday;2 intDaynumber = (int) today;3Console.WriteLine ("{0} is day number #{1}", today, daynumber);4 5Months Thismonth =Months.dec;6 byteMonthNumber = (byte) Thismonth;7Console.WriteLine ("{0} is month number #{1}", Thismonth, monthnumber);8 9 //Output:Ten //Monday is day number #1 One //Dec is month number #11

Benefits of using enumerations instead of numeric types:

    1. Explicitly specify which values are valid values for a variable for client code.
    2. In Visual Studio, IntelliSense lists the defined values. (Here is puzzled???? )

If the value of the element is not specified in the enumerator list, the value is automatically incremented by 1. In the previous example, the value of Days.sunday is 0,days.monday with a value of 1, and so on. When you create a new days object, it will have a default value of Days.sunday (0) If you do not explicitly assign it a value. When you create an enumeration, you should choose the most reasonable default value and assign it a value of 0. This makes all enumerations that are built will have that default value as long as they are not explicitly assigned when the enumeration is created.

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

1 days meetingday = days.monday; 2 // ... 3 meetingday = Days.sunday;

You can assign any integer value to Meetingday. For example, code line meetingday= (days) 42 does not produce an error. However, you should not do this because the default convention is that an enumeration variable accommodates only one of the values defined by the enumeration. Assigning any value to a variable of an enumeration type is likely to cause an error.

You can assign any value to an element in an enumerated list of enumerated types, or you can use a computed value:

1 enum machinestate 2  3     0,4     5, 5     Ten , 6     hibernating = sleeping+57 }

Enum type as bit flag

You can use an enumeration type to define a bit flag so that an instance of that enumeration type can store any combination defined in the list of enumerated columns. (Of course, some combinations may not be meaningful in the program code or are not allowed to be used.) )

The way to create a bit flag enumeration is to apply the System.FlagsAttribute attribute and define some values appropriately so that the and, or, not, and XOR bitwise operations can be performed on those values. The in-place flag enumeration contains a named constant with a value of 0, which means "No flags are set". If the value of 0 does not indicate "No flags are set", do not specify a 0 value for the flag.

(Note : Be sure to note that the flags here indicate that you can bitwise operate )

1 [Flags]2 enumDays23 {4None =0x0,5Sunday =0x1,6Monday =0x2,7Tuesday =0x4,8Wednesday =0x8,9Thursday =0x10,TenFriday =0x20, OneSaturday =0x40 A } - classMyClass - { theDays2 meetingdays = Days2.tuesday |Days2.thursday; -}

To set flags on an enumeration, use the bitwise OR operator, as shown in the following example:

1 //Initialize with the flags using bitwise OR.2Meetingdays = Days2.tuesday |Days2.thursday;3 4 //Set an additional flag using bitwise OR.5Meetingdays = Meetingdays |Days2.friday;6 7Console.WriteLine ("Meeting Days is {0}", meetingdays);8 //output:meeting Days is Tuesday, Thursday, Friday9 Ten //Remove a flag using bitwise XOR. OneMeetingdays = meetingdays ^Days2.tuesday; AConsole.WriteLine ("Meeting Days is {0}", meetingdays); - //output:meeting Days is Thursday, Friday

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

1 //Test value of the flags using bitwise AND.2 BOOLTest = (Meetingdays & days2.thursday) = =Days2.thursday;3Console.WriteLine ("Thursday {0} a meeting day.", test = =true?" is":" is not");4 //Output:thursday is a meeting day

Discovering and manipulating enumeration values using the System.Enum method

All enumerations are instances of the System.Enum type. You cannot derive a new class from System.Enum, but you can use its methods to discover information about the values in an enumeration instance and manipulate those values.

1 strings = Enum.getname (typeof(days),4);2 Console.WriteLine (s);3 4Console.WriteLine ("The values of the days Enum is:");5 foreach(intIinchEnum.getvalues (typeof(days)))6 Console.WriteLine (i);7 8Console.WriteLine ("The names of the days Enum is:");9 foreach(stringStrinchEnum.getnames (typeof(days)))TenConsole.WriteLine (str);

C # enum type

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.