"Go" writing high-quality Code 157 recommendations for improving C # programs--Recommendation 8: Avoid providing explicit values to elements of enumerated types

Source: Internet
Author: User

Recommendation 8: Avoid providing an explicit value to an element of an enumeration type

In general, it is not necessary to provide an explicit value to an element of an enumeration type. One reason to create an enumeration is to replace the actual numeric value. Incorrectly setting an explicit value for an element of an enumeration type can lead to unexpected errors.

If you add an element to the enumeration type week in recommendation 7, the code looks like this:

    enum Week      {          1,          2,          valuetemp,          3,           4 ,           5 ,           6 ,           7     

Imagine what the value of valuetemp would be? To verify the results, let's look at the output of this code:

    Week Week = week.valuetemp;      Console.WriteLine (week);      

The output is:

    Wednesday      

Unfortunately, we clearly assigned the value of week to valuetemp, but the result is Wednesday.

In fact, if you explicitly assign a value to an enumeration type, it is very likely that in the next release you will add elements to the enumeration for some additional needs, at which point, as we add element valuetemp for week, it is very likely that an invalid value will be accidentally added.

As already mentioned in the previous recommendation, if you do not explicitly assign a value to an element, the compiler will individually value +1 for the element. When the compiler discovers the element valuetemp, it automatically Tuesday = 2 on the basis of +1, so the actual valuetemp value and Wednesday value are 3. The enumeration elements that are included in the enumeration itself are value types, resulting in the output above.

From the above example we should have noticed that the enumeration element allows the setting of duplicate values. So, when we see the output of the following code, we should not be surprised:

 enum   Temp {Value1  = 1   = 1}  
    Private Static void NewMethod2 ()      {          = temp.value1;           = temp.value2;           = = Temp2);          Console.WriteLine (Temp1. Equals (TEMP2));          Console.WriteLine (Temp1.compareto (TEMP2));           = = temp.value1)          ; = = temp.value2)      ;

The output is:

True       true      0      true      

Note that there are exceptions to this recommendation. For example, when you specify the System.FlagsAttribute property for an enumeration type, it means that you can perform bitwise operations on and, or, not, and XOR on these values, so that the value of each element of the enumeration is required to be a number of powers of 2, and the exponent is incremented sequentially. For example, the week version should be:

[Flags]enumWeek {None=0x0, Monday=0x1, Tuesday=0x2, Wednesday=0x4, Thursday=0x8, Friday=0x10, Saturday=0x20, Sunday=0x40     }           classMyClass {Week Week= Week.thursday |Week.sunday; }

The output is:

Thursday, Sunday

Turn from: 157 recommendations for writing high-quality code to improve C # programs Minjia

"Go" writing high-quality Code 157 recommendations for improving C # programs--Recommendation 8: Avoid providing explicit values to elements of enumerated types

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.