ZZ Flag attribute for enum

Source: Internet
Author: User
Tags bitwise

Http://blog.sina.com.cn/s/blog_429fe72e010006u3.html

[SerializableAttribute] [AttributeUsageAttribute (Attributetargets.enum, inherited = False)] [ComVisibleAttribute (True)]public class Flagsattribute:attribute

Bit fields are typically used for lists that consist of elements that can be combined, and enumeration constants are typically used for lists that consist of mutually exclusive elements. Therefore, bit fields are designed to generate unnamed values by a bitwise OR operation combination, while enumeration constants are not. Language differs in the use of bit fields and enumeration constants.

Properties of the FlagsAttribute

AttributeUsageAttribute applies to this class, and its inherited property specifies false. This property can only be applied to enumerations.

Guidelines for FlagsAttribute and enums
  • The FlagsAttribute custom property is used for enumerations only when bitwise operations (and, or, XOR) are performed on numeric values.

  • The enumeration constants are defined with a power of 2 (that is, 1, 2, 4, 8, and so on). This means that the individual flags in the combined enumeration constant do not overlap.

  • Consider creating an enumeration constant for common flag combinations. For example, if an enumeration for file I/O operations contains enumerated constants Read = 1 and write = 2, consider creating enumeration constants ReadWrite = Read OR Write, which combines the Re Ad and Write flags. In addition, in some cases, a bitwise OR operation for a composite flag might be considered a high-level concept, which is not required in a simple task.

  • You should be cautious when defining negative numbers as flag enumeration constants, because many of the flag locations may be set to 1, which can confuse your code and make it prone to code errors.

  • A convenient way to test whether a flag is set in a numeric value is to perform a bitwise AND operation between a numeric value and a flag enumeration constant, which sets all bits in the numeric value that do not correspond to the flag to zero, and then tests whether the result of the operation equals the flag enumeration constant.

  • Use None as the name of the flag enumeration constant with a value of zero. In a bitwise AND operation, you cannot use the None enumeration constant to test the flag because the resulting result is always zero. However, you can perform a logical (not bitwise) comparison between a numeric value and a None enumeration constant to determine whether any bit is set in the numeric value.

    If you are creating a value enumeration instead of a flag enumeration, it is still useful to create the None enumeration constant. The reason is that by default, the common language runtime initializes the memory used for enumeration to zero. Therefore, if you do not define a constant with a value of zero, the enumeration will contain illegal values when it is created.

    If there is a noticeable default that the application needs to represent, consider using an enumeration constant with a value of zero to represent the default value. If there is no default, consider using an enumeration constant with a value of zero (which means that the condition is not represented by any other enumeration constant).

  • Do not define enumeration values just to reflect the state of the enumeration itself. For example, do not define enumeration constants that are only used to mark the end of an enumeration. If you need to determine the last value of the enumeration, explicitly check the value. In addition, if all values in the enumeration constant range are valid, you can also perform a scope check on the first and last enumerated constants.

  • Do not specify enumeration constants that are reserved for future use.

  • When you define a method or property that takes an enumeration constant as a value, you should consider validating the value. The reason is that a numeric value can be cast to an enumeration type even if it is not defined in the enumeration.

Example

The following code example illustrates how to use the FlagsAttribute property and shows the effect of the FlagsAttribute declared with the Enum on the ToString method.

Example of the FlagsAttribute attribute.
Using System;

Class Flagsattributedemo
{
Define an Enum without FlagsAttribute.
Enum Singlehue:short
{
Black = 0,
Red = 1,
Green = 2,
Blue = 4
};

Define an Enum with FlagsAttribute.
[FlagsAttribute]
Enum Multihue:short
{
Black = 0,
Red = 1,
Green = 2,
Blue = 4
};

static void Main ()
{
Console.WriteLine (
"This example of the FlagsAttribute attribute \ n" +
"generates the following output.");
Console.WriteLine (
"\nall possible combinations of values of an \ n" +
"Enum without flagsattribute:\n");

Display all possible combinations of values.
for (int val = 0; Val <= 8; val++)
Console.WriteLine ("{0,3}-{1}",
Val, ((Singlehue) val). ToString ());

        console.writeline (
              "\nall possible combinations of values of an \ n" +
              "Enum with flagsattribute:\n");
       
        //Display All possible combinations of values.
        //Also Display an invalid value.
        for (int val = 0; Val <= 8; val++)
    & Nbsp;       console.writeline ("{0,3}-{1}",
                 val, ((MultiHue) val). ToString ());
    }
}

/*
This example of the FlagsAttribute attribute
Generates the following output.

All possible combinations of values of
Enum without FlagsAttribute:

0-black
1-red
2-green
3-3
4-blue
5-5
6-6
7-7
8-8

All possible combinations of values of
Enum with FlagsAttribute:

0-black
1-red
2-green
3-red, Green
4-blue
5-red, Blue
6-green, Blue
7-red, Green, Blue
8-8
*/

Inheritance Hierarchy

System.Object
System.Attribute
System.FlagsAttributeThread Safety
Any public static of this type (in Visual Basic SharedMembers are thread-safe, but there is no guarantee that all instance members are thread-safe. Platform

Windows 98, Windows SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartpho NE, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Start ER Edition

The. NET Framework does not provide support for all versions of each platform. For a list of supported versions, see System requirements.

Version information
The. NET framework is supported by the following versions: 2.0, 1.1, 1.0
The. NET Compact Framework is supported by the following versions: 2.0, 1.0 see
Refer to FlagsAttribute Members
System namespace

ZZ Flag attribute for enum

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.