C # Enumeration type

Source: Internet
Author: User

EnumThe keyword is used to declare enumeration, which is a unique type consisting of a group of named constants called the enumerated number list. Each Enumeration type has a base type, which can be any integer except char. The default basic type of enumeration elements is int. By default, the value of the first enumeration number is 0, and the value of each enumeration number increases by 1. For example:

Copy
      enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};

In this enumeration,SatIs0,SunIs1,MonIs2, And so on. The number of enumerative values can have the default value of rewriting. For example:

Copy
      enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

In this enumeration, force the element sequence from1Instead0Start.

You canDaysA type variable is assigned any value within the range of the base type. The assigned value is not limited to a named constant.

EnumThe default value of E is the expression.(E) 0Generated value.

Note:

The name of the enumerated number cannot contain spaces.

The base type specifies the storage size allocated for each enumeration. HoweverEnumExplicit type conversion is required to convert data types to integer types. For example, the following statementEnumConvert to int to countSunVariables assigned to the int type:

Copy
int x = (int)Days.Sun;

When system. flagsattribute is applied to an enumeration, if the enumeration contains elements that use bitwise "or" Operation combinations, you will notice that this attribute will be affected when used in some tools.Enum. When you useConsoleWhen using tools such as class methods and expression calculators, you can notice these changes. (See example 3 ).

Reliable Programming

If you assign another value to the new version of enumeration, or change the value of the enumerated members in the new version, the source code may be problematic. Generally, the switch statement usesEnumValue. If other elements have been addedEnumType, the default value test may return true unexpectedly.

If other developers will use your code, you need to provide instructions to inform developers to add new elements to anyEnumType, how should their code respond.

Example

In this example, an enumeration is declared.Days. The two enumerated numbers are explicitly converted to Integers and assigned to integer variables.

Copy
// keyword_enum.cs// enum initialization:using System;public class EnumTest {    enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};    static void Main()     {        int x = (int)Days.Sun;        int y = (int)Days.Fri;        Console.WriteLine("Sun = {0}", x);        Console.WriteLine("Fri = {0}", y);    }}
Output
Sun = 2Fri = 7

In this example, the base class option is used to declare that the member type isLongOfEnum. Note that even if the basic Enumeration type isLong, Enumeration members must still use forced conversion to explicitly convertLongType.

Copy
// keyword_enum2.cs// Using long enumeratorsusing System;public class EnumTest {    enum Range :long {Max = 2147483648L, Min = 255L};    static void Main()     {        long x = (long)Range.Max;        long y = (long)Range.Min;        Console.WriteLine("Max = {0}", x);        Console.WriteLine("Min = {0}", y);    }}
Output
Max = 2147483648Min = 255

The following code exampleEnumDeclaredSystem. flagsattributeAttribute usage and effect.

Copy
// enumFlags.cs// Using the FlagsAttribute on enumerations.using System;[Flags]public enum CarOptions{    SunRoof = 0x01,    Spoiler = 0x02,    FogLights = 0x04,    TintedWindows = 0x08,}class FlagTest{    static void Main()    {        CarOptions options = CarOptions.SunRoof | CarOptions.FogLights;        Console.WriteLine(options);        Console.WriteLine((int)options);    }}
Output
SunRoof, FogLights5
Note

NOTE: IfSat = 1And the result is:

Copy
Sun = 1Fri = 6
Note

Note that if you removeFlagsattribute, The output in this example is:

5

5

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.