Tips for designing and using C # enum

Source: Internet
Author: User
Tags bitwise constant numeric numeric value

The operation of an enum usually involves a bitwise operation (and, or, XOR), often using the FlagsAttribute custom attribute definition. Guidelines for FlagsAttribute and enums:

FlagsAttribute Custom attributes are used for enumerations only when bitwise operations (and, or, XOR) are performed on numeric values.

Define the enumeration constants with a power of 2 (that is, 1, 2, 4, 8, and so on). This means that each of the flags in the combined enumeration constants does not overlap.

Consider creating an enumerated constant for a common flag combination. For example, if the enumeration used for file I/O operations contains enumerated constants Read = 1 and write = 2, consider creating an enumeration constant ReadWrite = Read or write, which combines the read and write flags. In addition, in some cases, the bitwise OR operation used to combine flags may be considered an advanced concept that you do not need to perform in simple tasks.

You should be cautious about defining negative numbers as flags enumeration constants, because many flag locations can be set to 1, which can confuse your code and make code errors easier.

An easy way to test whether a flag is set in a numeric value is to perform a bitwise and action between numeric and flag enumeration constants, 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 an enumeration constant for a flag with a value of zero. In bitwise AND operations, you cannot use the None enumeration constant to test flags because the resulting result is always zero. However, you can perform a logical (not bitwise) comparison between the numeric value and the None enumeration constant to determine whether any bits have been set in the value.

Creating a None enumeration constant is still useful if you are creating a value enumeration instead of a flag enumeration. The reason is that the common language runtime initializes the memory used for enumerations to zero by default. 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 are obvious defaults that the application needs to represent, consider using an enumerated constant with a value of zero to represent the default value. If no default is present, consider using an enumeration constant with a value of zero (this means that the case is not represented by any other enumerated constants).

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

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

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

Tip 1: How to remove an enumerated item:

For example, the permission enum Permission is defined:

[Flags]

public enum Permission{
   Select = 1,
   Edit = 2,
   Delete = 4,
   View = 8,
   All = Select | Edit | Delete | View
 } 

This function can be used to calculate:

public static Permission ClearFlag(Permission value, Permission flag)
{
  value = value & (Permission.All^ flag);
  return value;
}

Http://www.cnblogs.com/shanyou/archive/2006/11/16/562816.html

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.