CLR Note: 12. Enumeration type and Bit Flag

Source: Internet
Author: User
Tags bit set

12.1 Enumeration type
Strong type, which cannot be implicitly converted between enumerations
The enumerated type is directly derived from System. Enum, and the latter is derived from System. ValueType. The value type can be boxed or unboxed.
Methods, properties, and events cannot be defined.

In the same enumeration, multiple enumeration symbols have the same value. When a value is converted to a symbol, the first symbol is returned.
The enumeration type must be at the same level as the class that uses it.

The default value is int. You can specify only int, uint, byte, sbyte, long, ulong, short, and ushort primitive types for enumeration members.
The operator can be used to act on enumeration types. In fact, it acts on the corresponding value instance fields, such as ++.
You can convert an instance of the enumeration type to another different Enumeration type. In fact, the instance is converted to a value first, and then finds the corresponding enumerated value based on the value in another enumeration.
You can convert an instance of the enumeration type to a numeric type. Public enum Color

Public enum Sentence

Class Program
Static void Main (string [] args)
// Use the operator, ++
Color a = Color. Red;

Sentence s;
// Two different enumerations are converted to each other, but the Color. White value is 2, which is not found in Sentence. Therefore, an integer 2 is directly returned.
S = (Sentence) Color. White; // equivalent to (Sentence) 1;

// Two different enumerations are converted to each other. The return value is Black.
S = (Sentence) Color. Black;

// Directly output the White enumerated value and convert it to an integer.
Console. WriteLine (Color. White); // output white
Console. WriteLine (Int32) Color. White); // output 2

For example, enum Color {Red = 1, White = 2} corresponds to IL:

As you can see, enumeration is only a structure in which a series of constant fields and instance fields are defined.
If you only use a type such as Color. Red, it will be processed as a constant, so that the Code does not reference the defined Enumeration type, you can delete the corresponding dll (See Chapter 7th );
If the syntax such as Color c = new Color (); is used, it depends on references and cannot delete the corresponding dll.

Enumeration Method list:

1. GetUnderlyingType: Obtain the core type of the enumerated type value:

// Return System. Int
Enum. GetUnderlyingType (typeof (Color ));

2. ToString () method

Color c = Color. Black;

Console. WriteLine (c); // output Black
Console. WriteLine (c. ToString (); // output Black
Console. WriteLine (c. ToString ("G"); // output Black, all of which are in generic format.
Console. WriteLine (c. ToString ("D"); // output 1, decimal
Console. WriteLine (c. ToString ("X"); // output 00000001, hexadecimal

For c. ToString ("X") hexadecimal format, the A-F is always capital letters. In addition, the number of output digits depends on the basic Color type:
Byte/sbyte 2-bit
Short/ushort 4-bit
Int/uint 8-bit
Long/ulong 16-bit

3. Format static method. The function is basically the same as the ToString () method, but the value is allowed to pass a value, not just an Enum type.

Enum. Format (typeof (Color), 3, "G ");

4. The GetValues static method returns an array with names of all enumeration types in the array:

Color [] colors = (Color []) Enum. GetValues (typeof (Color ));

5. static String GetName (Type enumType, Objbect value) static method, returns the String representation of the value
6. static String [] GetNames (Type enumType), returns the array representation of Method 5
7. Parse static method, which converts a symbol to an instance of Enumeration type:

Color C0 = (Color) Enum. Parse (typeof (Color), "2 ");
Color cc2 = (Color) Enum. Parse (typeof (Color), "Black"); // It Can Be A value or enumeration type name
Color cc3 = (Color) Enum. Parse (typeof (Color), "Black", true); // optional parameter true indicates case-insensitive

8. IsDefined static method to determine whether a value is valid for an enumeration type:

Enum. IsDefined (typeof (Color), 10); // return false, Color does not contain 10
Enum. IsDefined (typeof (Color), "Black"); // return true

It is often used for parameter verification:

Public void SetColor (Color c)
If (! Enum. IsDefined (typeof (Color), c ))
// Throw an exception. c is not suitable for Color.

Note: The Enum. IsDefined method should be used with caution, because case-sensitive search is required; reflection mechanism is used, and the speed is slow.

9. toObject static method, a series of methods, such as Enum. toObject (Type, Byte), convert Int32, Byte, and other types of instances to Enumeration Type instances (because the returned results are all objects, it is necessary to display the transformation)

12.2-bit flag
The bitwise mark is a single-digit set, where some are on and some are off, while enumeration is a numerical value, which is the accumulation of several values in the bit set.
When the [Flags] attribute is applied to enumeration, enumeration can be used to represent a group of BITs that can be combined. Generally, a zero-value None symbol must be defined.

Public enum FileAttributes
ReadOnly = 0x0001,
Hiding = 0x0002,
System = 0x0004

In this case, bitwise operations can be used:

Logic and, determine whether this enumerated value exists // the following code first retrieves the object attribute list-a single-bit set,
// Logic with FileAttributes. Hidden: If FileAttributes. Hidden exists in the bit set, FileAttributes. Hidden is returned.
String file = @ "C: \ Boot. ini ";
FileAttributes attributes = File. GetAttributes (file );
If (attributes & FileAttributes. Hidden) = FileAttributes. Hidden ){}

Logical or, used to combine the required enumerated list, which is generally used to set:

File. SetAttributes (path, FileAttributes. ReadOnly | FileAttributes. Hidden );

Call the ToString () method to mark Enum, and convert the value to the string:
First, check whether the [Flags] attribute is applied.
If no, search for the Enum symbol that matches the value and return the value. If no matching is found, return the value.
If yes, obtain the value set A in the descending order of Enum, and append the Enum item whose result is equal to the value in the value "bitwise AND" Enum instance "of A to the output string, separated by commas, such as "Read, Write"
There are two special cases:
1. If a value cannot be combined by multiple Enum items, this value is returned.
2. Define 0.

Call the Parse () method to mark Enum, and convert the string to a value:

Enum. Parse (typeof (Color), "Query ");
Enum. Parse (typeof (Color), "Query, Read ");
Enum. Parse (typeof (Color), "28 ");

The process is as follows:
1. Delete all blank characters in the string
2. If the string starts with a +/-/number, the string is considered to be a number and is directly transformed into a string corresponding to the value.
3. Split the string with commas (,), search for the value of the corresponding symbol in Enum, and return the value by bit or value.

Do not Mark Enum to call the IsDefined () method because,
String. The bits contain commas (,), but the enumerated characters containing commas (,) are never found;
Pass a value, always return false

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: 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.