【Abstract】 enumeration Design

Source: Internet
Author: User

Http://msdn.microsoft.com/zh-cn/library/ms229058 (vs.80). aspx

Http://msdn.microsoft.com/zh-tw/library/sbbt4032.aspx

Enumeration Design

Enumeration provides Group constant values, which help to make members of a strong type and improve code readability. Enumeration can be classified into simple enumeration and flag enumeration. Values contained in simple enumeration are not used for combination or bitwise comparison. Mark enumeration should be combined with bitwise OR operations. Use the bitwise AND operation to check the combination of flag enumerated values.

The following guidelines describe best practices for enumeration design.

You must use enumerative strongly typed parameters, attributes, and return values of Value Sets. You must use enumeration instead of static constants.

The following code example demonstrates incorrect design.

C # copy code

public static class BadFurnishings            {            public static int Table = 1;            public static int Chair = 2;            public static int Lamp = 3;            }            

The following code example demonstrates how to replace the enumeration of static constants.

C # copy code

public enum GoodFurnishings            {            Table,            Chair,            Lamp            }            
Do not use enumeration for Open Sets (such as operating system versions.

Adding a value to the provided enumeration interrupts the existing code. This practice can be accepted sometimes, but enumeration should not be designed when such a situation may occur.

Do not define reserved enumerated values for future use.

In some cases, you may think that adding values to the provided enumeration is worth the risk of interrupting the existing code. You can also define new enumerations and members that use their values.

Avoid exposing enumeration with only one value. Do not include the Sentinel value in enumeration.

The Sentinel value is used to identify the boundary of the value in the enumeration. Generally, the Sentinel value is used for range check. It is not a valid data value. The following code example defines an enumeration with Sentinel values.

C # copy code

public enum Furniture            {            Desk,            Chair,            Lamp,            Rug,            LastValue   // The sentinel value.            }            
Make sure to provide a zero value in simple enumeration.

If possible, name this valueNone. IfNoneNot suitable. Assign the zero value to the most common value (default value ).

Consider using system. int32 (the default data type in most programming languages) as the underlying data type for enumeration, unless any of the following situations occurs:
  • Enumeration is a flag enumeration, And you have more than 32 logos or expect more in the future.

  • The basic type must be different from int32 to facilitate interoperability with the unmanaged code that expects enumeration of different sizes.

  • A small basic type can save a lot of space. If enumeration is expected to be mainly used as a control flow parameter, its size is not very important. If the following situations occur, it may be important to save the size:

    • An enumeration is expected to be used as a field in a structure or class that is frequently instantiated.

    • You are expected to create a large array or set of enumeration instances.

    • A large number of enumeration instances are expected to be serialized.

Be sure to name the marker enumeration by the plural number of nouns or phrases. A simple enumeration should be named by a singular noun or noun phrase. Do not directly extend system. enum.

Some compilers do not allow extension of Enum unless indirectly using language-specific keywords that generate enumeration for extension.

Part of the copyright is 2005 Microsoft Corporation. All rights reserved.

Some copyrights are reserved for Addison-Wesley Corporation. All rights reserved.

For more information about the Design Guide, see Framework Design Guidelines: Conventions, idioms, and patterns for reusable, edited by Krzysztof cwalina and Brad Abrams and published by Addison-Wesley in 2005. net libraries (Framework Design Guide: reusable. net Library conventions, terms, and patterns).

See concepts

Design flag Enumeration
Add value to Enumeration
Other resources

Type Design Principles
Design Guidelines for Class Library Development

 

 

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.