There are two ways to define constants in C #, one is called a static constant (Compile-time constant), and the other is a dynamic constant (Runtime constant). The former is defined by "const", which is defined by "ReadOnly".
For static constants (Compile-time constant), it is written in the following ways:
public const int max_value = 10;
Why call it a static constant, because as above the declaration can be understood as follows (note: The following writing is wrong, there will be a compilation error, here just for the convenience of explanation).
public static const int max_value = 10;
Constants defined with const are the same for all class objects, so you need to access the const-defined constants as you would access the static members, and the members of the object to access the variable errors. In addition, access to static constants is used to replace constants with constant values when compiling, for example:
int nvalue = Max_value;
After compiling, this sentence is the same as the intermediate language code produced by the following sentence.
int nvalue = 10;
However, there are a number of limitations on types when defining constants with Const. First, this type must belong to a value type, and initialization of this type cannot be done by new, so some value type constants defined with a struct cannot be defined with Const.
In contrast to const, it is much more flexible to use ReadOnly to define constants, which are written in the following ways:
public readonly int max_value = 10;
Why is it called a dynamic variable, because the system allocates space for constants defined by ReadOnly, which is the same as other members of the class. In addition, the constants defined by ReadOnly can be set in the constructor of the class, in addition to the constant values defined at the time of definition. Since the constants defined by ReadOnly are equivalent to the members of a class, using const to define the type limitations of constants is eliminated when you use ReadOnly to define them, which means you can use ReadOnly to define constants of any type.
As described above, the difference between the two is as follows.
As for defining constants, whether they are defined by const or READONLY, I used to define them as const in order to pursue performance. In this book, however, a potential bug is raised about the use of Const. When using the static constants of a class in a DLL class library in a program, if the value of the static constant is modified in the class library, the other interfaces do not change, in general, the program caller does not need to recompile, directly execute can call the new class library. In this case, however, a potential bug is generated. This is because static constants are used to replace constants at compile time, so the program at the end of the call is replaced.
For example: a static constant is defined in the class library, as follows:
public const int max_value = 10;
So for the code that calls this static constant in the program, the intermediate language code produced after compilation is replaced with 10来, where static constants are used, and changed to 10.
Then, when the static variables of the class library change, for example:
public const int max_value = 15;
Then for the caller the program can run without recompiling, but at this point the program's intermediate language code corresponds to the value of the static variable is 10, not the new class library 15. Therefore, the resulting inconsistency, the program will cause potential bugs. The solution to this type of problem is that the caller is recompiled after updating the class library, generating a new intermediate language code.
For the above potential bugs in Const definition constants, this will not occur when defining constants with ReadOnly. Because the constants defined by ReadOnly are similar to members of a class, they need to be accessed at a specific constant address to avoid such bugs.
In view of this, the book suggests replacing Const with ReadOnly to define constants.
Two methods of defining constants in C #