C # tips for designing and using Enum

Source: Internet
Author: User

C #OfEnumDesign-related guidance is available on the InternetRelatedArticleDiscussion:

English:Design Guidelines update: Enum Design

Chinese:C # idioms: EnumOrEnum class (Enumeration class)

Enum Operations generally involve in-place operations ( And , Or , XOR ), Bitwise operation , Frequently Used Flagsattribute Custom Attributes. Flagsattribute And Enum Principles:

  • only bitwise operations ( and , or , XOR ) flagsattribute Custom Attributes.

  • with 2 power (that is, 1 , 2 , 4 , 8 ) defines enumerated constants. This means that each flag in the enumerated constant of the combination does not overlap.

  • Consider creating an enumerated constant for a combination of common signs. For example I/O The enumerated values of the operation include enumeration constants. Read = 1 And Write = 2 , Consider creating enumerated Constants Readwrite = read or write, This constant is combined Read And Write Flag. In addition, in some cases, it may be used to combine the tag by bit Or An operation is considered as an advanced concept. You do not need to perform this operation in a simple task.

  • Exercise caution when defining a negative number as a flag enumeration constant, because many flag locations may be set1, Which may make yourCodeObfuscation and easy code errors.

  • To test whether a flag has been set in a value, perform the bitwise "and" operation between the value and the enumerated constant, in this method, all bits in the value that do not correspond to the flag are set to zero, and then the result of this operation is equal to the enumerated constant of the flag.

  • to none used as a flag for zero value Enumeration Volume name. In the bitwise and in operation, cannot Use none constant Enumeration Test mark, the result is always zero. However, you can compare the value with none execution logic between enumerated constants (not by bit) to determine whether any bits have been set in the value.

  • If you create a value enumeration instead of a flag enumeration, create NoneEnumeration constants are still very useful. The reason is that by default, the common language runtime will initialize the memory for enumeration to zero. Therefore, if a constant with a zero value is not defined, enumeration will contain an invalid value at creation.

  • If an application existsProgramTo indicate the default value, consider using an enumeration constant with zero value to represent the default value. If no default condition exists, consider using an enumeration constant whose value is zero (which means that this condition is not represented by any other enumeration constant ).

  • Do not define enumeration values only to reflect the enumeration status. For example, do not define an enumerated constant that is 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 enumerated constant range are valid, you can also perform a range check on the first and last enumerated constants.

  • Do not specify the enumerated constants reserved for future use.

  • When defining a method or attribute that uses an enumerated constant as a value, you should consider verifying the value. The reason is that even if a value is not defined in the enumeration, the value can be forcibly converted to the enumeration type.

Tips1: How to remove an enumeration item:

 For example, a permission is defined.EnumPermission:

[Flags]

Public Enum Permission{

 Select = 1,

 Edit = 2,

Delete = 4,

View = 8,

All = select | edit | Delete | View

  }

You can use this function for calculation:

Public static permission clearflag (permission value, permission flag) 

{

Value = Value & (permission. All ^ flag );

 Return value;

}

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.