Enum (C # reference)

Source: Internet
Author: User
Enum (C # reference)

 

 

The enum keyword is used to declare enumeration, which is a unique type consisting of a group of named constants called the enumeration list.

Generally, it is better to define an enumeration in the namespace so that all classes in the namespace can access it easily. However, enumeration can also be nested in a class or structure.

By default, the value of the first enumeration number is 0, and the value of each enumeration number increases by 1. For example:

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

In this enumeration, sat is 0, Sun is 1, mon is 2, and so on. The number of enumerative values can have the default value of rewriting. For example:

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

In this enumeration, the force element sequence starts from 1 rather than 0. However, it is strongly recommended that the enumeration contains a constant with a value of 0. For more information, see {
Track ('ctl00 _ rs1_maincontentcontainer_ctl00 | ctl00_rs1_maincontentcontainer_ctl03 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/cc138362.aspx "> Enumeration type (C # programming guide ).

Each Enumeration type has a basic type, which can be a division {
Track ('ctl00 _ rs1_maincontentcontainer_ctl00 | ctl00_rs1_maincontentcontainer_ctl04 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/x9h8tsay.aspx "> any integer other than char. The default basic type of enumeration elements is {
Track ('ctl00 _ rs1_maincontentcontainer_ctl00 | ctl00_rs1_maincontentcontainer_ctl05 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/5kzh1b5w.aspx "> Int. To declare another integer enumeration, for example {
Track ('ctl00 _ rs1_maincontentcontainer_ctl00 | ctl00_rs1_maincontentcontainer_ctl06 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/5bdb6693.aspx "> byte, follow the type after the identifier, and then use the colon:

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

Allowed enumeration types include byte ,{
Track ('ctl00 _ rs1_maincontentcontainer_ctl00 | ctl00_rs1_maincontentcontainer_ctl08 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/d86he86x.aspx "> sbyte ,{
Track ('ctl00 _ rs1_maincontentcontainer_ctl00 | ctl00_rs1_maincontentcontainer_ctl09 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/ybs77ex4.aspx "> short ,{
Track ('ctl00 _ rs1_maincontentcontainer_ctl00 | ctl00_rs1_maincontentcontainer_ctl10 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/cbf1574z.aspx "> ushort ,{
Track ('ctl00 _ rs1_maincontentcontainer_ctl00 | ctl00_rs1_maincontentcontainer_ctl11 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/5kzh1b5w.aspx "> int ,{
Track ('ctl00 _ rs1_maincontentcontainer_ctl00 | ctl00_rs1_maincontentcontainer_ctl12 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/x0sksh43.aspx "> uint ,{
Track ('ctl00 _ rs1_maincontentcontainer_ctl00 | ctl00_rs1_maincontentcontainer_ctl13 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/ctetwysk.aspx "> long or {
Track ('ctl00 _ rs1_maincontentcontainer_ctl00 | ctl00_rs1_maincontentcontainer_ctl14 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/t98873t4.aspx "> ulong.

A variable of the days type can be assigned any value within the range of the basic type. The assigned value is not limited to the named constant.

The default value of Enum e is the value generated by expression (e) 0.

Note:

The name of the enumerated number cannot contain spaces.

The base type specifies the storage size allocated for each enumeration. However, the conversion from the enum type to the integer type requires explicit type conversion. For example, the following statement uses forced conversion (from Enum to int) to assign the enumerated number Sun to {
Track ('ctl00 _ rs1_maincontentcontainer_ctl00 | ctl00_rs1_maincontentcontainer_ctl15 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/5kzh1b5w.aspx "> int type variables:

int x = (int)Days.Sun;

Set {
Track ('ctl00 _ rs1_maincontentcontainer_ctl00 | ctl00_rs1_maincontentcontainer_ctl17 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.flagsattribute.aspx "> system .. ::. when flagsattrinum is applied to an enumeration, if the enumeration contains elements that use a bitwise OR operation combination, you will notice that this attribute affects the behavior of Enum when used in some tools. When using {
Track ('ctl00 _ rs1_maincontentcontainer_ctl00 | ctl00_rs1_maincontentcontainer_ctl19 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.console.aspx "> note these changes when using tools such as the console class method and expression calculator. (See example 3 ).

Reliable Programming

Like any constant, all references to values in the enumeration are converted to numeric text during compilation. This may lead to potential version control problems, such {
Track ('ctl00 _ rs1_maincontentcontainer_cpe102091_c | ctl00_rs1_maincontentcontainer_ctl24 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/ms173119.aspx "> constants (C # programming guide.

Assigning other values to the new version of enumeration, or changing the values of enumeration members in the new version can cause problems in the source code. Usually in {
Track ('ctl00 _ rs1_maincontentcontainer_cpe102091_c | ctl00_rs1_maincontentcontainer_ctl25 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/06tc147t.aspx "> Use enumeration values in switch statements. If other elements are added to the enum type, the default value test may return true if it is not expected.

If other developers will use your code, you need to provide instructions to inform developers of how their code responds if new elements are added to any Enum type.

Example

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

C #
public class EnumTest{enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };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 = 0Fri = 5*/

In this example, the base class option is used to declare the enum whose member type is long. Note that even if the basic Enumeration type is long, you must use force conversion to explicitly convert the enumeration member to the long type.

C # public class enumtest2
{
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 = 2147483648
Min = 255
*/

The following code example illustrates {
Track ('ctl00 _ rs1_maincontentcontainer_cpe102092_c | ctl00_rs1_maincontentcontainer_ctl35 ', this );
} "Href =" http://msdn.microsoft.com/zh-cn/library/system.flagsattribute.aspx "> system...: Use and effect of the. flagsattribute attribute.

C #
[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 that if flagsattribute is removed, 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.