[CLR via C #] 15. Enumeration type and Bit Flag

Source: Internet
Author: User


Enumerated types defines a pair of "Symbol names/values.
For example, the following Color type defines a set of symbols, each of which identifies a Color:

 Red,          Green,        Blue,         Orange        }

  

1) Enumeration type makes the program easier to write, read, and maintain. With the enumeration type, the symbol name can be used in the Code. Developers do not need to remember the meaning of each hard encoding. In addition, once the value corresponding to the symbol name changes, the code can be easily re-compiled without any modification to the source code. In addition, document tools and other utilities can display meaningful symbolic names to developers.

2) Enumeration type is strongly typed.

In Microsoft. NET Framework, the enumeration type is not only a symbol concerned by the compiler, but also a "first-class citizen" location in the type system, which can implement very powerful operations.

When compiling enumeration types, the C # compiler converts each symbol into a constant field of the type. For example, the compiler regards the Color Enumeration type as the following code:

     Color While = (Color)  Color Red = (Color)  Color Green = (Color)  Color Bule = (Color)  Color Orange = (Color)  

The preceding pseudo-type definition defines how internal work is performed. In short, the enumeration type is only a structure, which defines a set of constant fields and an instance segment. Constant fields are embedded in the Assembly metadata and can be accessed through reflection. This means that all symbols associated with the enumeration type and their values can be obtained at runtime. It also means that a string symbol can be converted into a corresponding value. These operations are performed through System. the Enum base type is provided. This type provides several static and instance methods that can be used to operate an instance of the enumeration type, thus avoiding the need to use reflection.

Annoying.

Tip: The symbol defined by the enumeration type is a constant value. Therefore, when the compiler finds that the code references an enumeration type symbol, it will replace the symbol with a numerical value during compilation, and the code will no longer reference the enumeration type of the symbol. This means that you do not need to define an assembly of the enumeration type during runtime.

Required during compilation.

For example, the System. Enum Type has a static GetUnderlyingType method, while the System. Type has a GetEnumUnderlyingType instance method.

  Type GetUnderlyingType (Type enumType);  Type GetEnumUnderlyingType (Type enumType); 

Short, ushort, int (most commonly used, also C # default), uint, long, or ulong. Although all these C # primitive types have the FCL type of the object, the C # compiler must only specify the primitive type name to simplify its implementation. If the FCL type name is used (for example, Int32 ),

Error.
The following Code declares an enumeration type whose basic type is byte (System. Byte:

inter  Color :

Console.WriteLine(Enum.GetUnderlyingType((Color)));

All these operators actually act on the value _ instance field inside the enumeration type instance. In addition, the C # compiler also explicitly converts an instance of the enumeration type to an invalid Enumeration type. You can also explicitly convert an enumerated instance to a numeric type.

You can call System. the static method GetValue or System of Enum. the Type instance method GetEnumValue gets an array. Each element of the array corresponds to a symbol name in the enumeration Type, and each element contains the value of the symbol Name:

  Array GetValues(Type enumType);  Array GetEnumValues(Type enumType); 

  = (Color[])Enum.GetValues( + (Color color     Console.WriteLine(

Number of symbols defined: 5
Value Symbol
-----------
0 While
1 Red
2 Green
3 Blue
4 Orange

Other enumeration type members are not described here!


Programmers often deal with bit flag sets. Call the GetAttributes method of the System. IO. File type to return an instance of the FileAttributes type. The FileAttributes type is the Int32 Enumeration type. Each bit reflects an attribute of the file. The definition of the FileAttibutes type in FCL is as follows:

 = = =     Directory = = ,     Hidden = = = = = = = = =  NET_4_5= = 

String file ==,file,(attributes & FileAttributes.Hidden) !=);

File.SetAttributes(file,FileAttributes.ReadOnly | FileAttribute.Hidden);

When defining the enumeration type used for the flag, assign a value to each symbol explicitly. Generally, each symbol has a single bit in the on (1) State. In addition, it is often necessary to define a zero-value None symbol. You can also define symbols that represent common bitwise combinations. In addition, we strongly recommend that you apply the custom Attribute type System. Flags. attribute to the enumeration type, as shown below:

 = = = Actions.Read |= = = 

Actions actions = Actions.Read | Actions.Delete; Console.WriteLine(actions.ToString()); 

Do not use the IsDefined method for the counterpoint flag Enumeration type for the following reasons:
1) if a string is passed to the IsDefined method, it will not split the string into separate tokens for search, but the view will look for the entire string, think of it as a larger symbol containing a comma. You cannot define a comma-containing symbol in the enumeration type, so this symbol will never be found.
2) If you pass a value to the IsDefined method, it checks whether the enumeration type defines a matching symbol between its corresponding value and the passed value. Because the bit flag cannot be simply matched, IsDefined usually returns flase.



Now, you can use the C # Extension Method to add methods to Enumeration type simulation.
To add some methods to the FileAttributes Enumeration type, you can define a static class containing the extension method, as shown below:

  Boolean Set( flags |

FileAttributes fa == fa.Set(FileAttributes.ReadOnly);

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.