Reliable programming of enums and constants in C #

Source: Internet
Author: User

look at the introduction of the enum in Microsoft's C # Reference when the following paragraph pops up:Reliable Programmingas with any constant, all references to the values in the enumeration are converted to numeric literals at compile time. This can form a potential versioning problem, as described in constants (C # Programming Guide) .

switch statements. " The enumeration values are typically used in switch statements. enum type, the default sections of the switch statement can be SELECTE D unexpectedly. " > If additional elements are added to the enum type, the default section of the switch statement may be unexpectedly selected.

If other developers use your code, you need to provide instructions that tell the developer how their code should respond if new elements are added to any enum type.

What the hell is this??

Carefully pondering, there may be the following situation:

In an assembly A, some constants are defined:

 Public Const int 1 ;  Public Const int 3 ;  Public Const int 4;

These constants are referenced in another assembly B:

        Switch(iTable) {//TableA             Casetablea:dosomething ();  Break; //TableB             Casetableb:dosomething ();  Break; //Tablee             Casetablee:dosomething ();  Break; //default            default: DoSomething ();  Break; }

This code uses variable itable to treat different table differently.

Everything is fine by now, no problem.

One day there may be some demand changes or someone is simply out of the mood, change assembly a a bit, as follows:

 Public Const intTableA =1; Public Const intTableB =2; Public Const intTableC =3; Public Const inttabled =4; Public Const intTablee =5;

After compiling, the *.dll of assembly a required in the Assembly B project is replaced, run, without error. Hey! All ok!!

We look back at this sentence: As with any constant, all references to the values in the enumeration are converted to numeric literals at compile time.

What does that mean??

In fact, this means that the compiled code of assembly B is equivalent to the following code:

        Switch(iTable) {//TableA             Case 1: DoSomething ();  Break; //TableB             Case 2: DoSomething ();  Break; //Tablee                 Case 5: DoSomething ();  Break; //default            default: DoSomething ();  Break; }

At this point, the problem is:

    • Itable is the TableA value, yes, the program enters case 1;
    • Itable is the TableB value, the program enters default; Since the value of TableB is changed to 2, there is no such branch. )
    • Itable is the Tablee value, the program still enters default; (for the same reason as TableB. )

This is the real meaning of the section in the C # Reference. It is also easy to solve this problem: it is best to recompile the DLL file referenced by the Assembly B Project when it is updated.

The RECOMPILE assembly B is equivalent to the following code:

        Switch(iTable) {//TableA             Case 1: DoSomething ();  Break; //TableB             Case 2: DoSomething ();  Break; //Tablee             Case 5: DoSomething ();  Break; //default            default: DoSomething ();  Break; }

In this way, everything is really OK!!

(Purely theoretical studies, no tests.) All the bad consequences, no responsibility! )

Reliable programming of enums and constants in C #

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.