[C # advanced series] 14 enumeration types and bits,

Source: Internet
Author: User

[C # advanced series] 14 enumeration types and bits,

In fact, this chapter only describes the enumeration type, because the bit flag can be used as a Special Enumeration type.

Enumeration type

Enumeration is a good way to eliminate magic numbers, making it easier for programs to write, read, and maintain.

The enumeration type is a value type. However, unlike other value types, the enumeration type cannot define any methods. Unless the extension method is used.

However, as I mentioned earlier, when you want to use the extension method, you can first consider whether the enumeration type is suitable for this place, or is this method suitable for adding to this enumeration type.

When compiling the enumeration type, the C # compiler has converted each symbol into a constant field of the type.

C # by default, the base type of a value containing an enumeration type is int, which can be customized as follows:

Enum hero type: byte {hero of strength, agile hero, hero of intelligence}

You can use the System. Enum. GetUnderlyingType method to obtain the basic type of the enumerated value. However, only some primitive types can be used here, And the FCL type cannot be used.

Usually, in addition to comparison, you can only convert it to a string to view the specific options (Other formatted output enumerations do not really work, so we will not list them here ):

Hero type heroType = hero type. Powerful hero; Console. WriteLine (heroType. ToString (); // powerful hero

You can also use the static method GetValues of Enum to return an array containing all the Enum options.

You can also use the static IsDefined method of Enum to check whether a value or its constant value is defined in the enumeration type. (However, This is slow because reflection is used in it)

Bit mark

As mentioned above, bit flag can be used as a Special Enumeration type.

[ComVisible(true)]    [Flags]    public enum FileAttributes    {        ReadOnly = 1,        Hidden = 2,        System = 4,        Directory = 16,        Archive = 32,        Device = 64,        Normal = 128,        Temporary = 256,        SparseFile = 512,        ReparsePoint = 1024,        Compressed = 2048,        Offline = 4096,        NotContentIndexed = 8192,        Encrypted = 16384,        IntegrityStream = 32768,        NoScrubData = 131072    }

In the preceding type, the binary value of 1 is 100, and the binary value of 4 is.

That is to say, each binary bit can be used to confirm the uniqueness. This is the principle of Bit Flag.

Var tmp = FileAttributes. ReadOnly | FileAttributes. Hidden; // indicates that the file is read-only and Hidden if (tmp & FileAttributes. ReadOnly )! = 0) {// because binary 1 & 1 is 1, the final value must not be 1 Console. WriteLine ("tmp read-only ");}

View the preceding definition of FileAttributes and you will find a [Flags] feature. This feature is used when tmp. ToString () is a "ReadOnly, Hidden" string.

If the [Flags] feature is not available, it is a specific value.

In the above sections, We will convert the enumerated type and bit flag to a string or value. to convert the string and value to an enumerated type or bit flag, we can use the following method:

Enum. TryParse <FileAttributes> ("ReadOnly, Hidden", false, out tmp); // false indicates case-insensitive. Here it is a bit mark, and the type of conversion to enumeration is similar.

 

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.