C # defines constants two implementation methods _ Practical Tips
Source: Internet
Author: User
There are two ways to define constants in C #, one called Static constants (Compile-time constant) and the other is called Dynamic constants (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 do you call it a static constant because, as the above declaration can be interpreted as follows (note: The following writing is wrong, it will be compiled incorrectly, just for the convenience of the description).
public static const int max_value = 10;
Constants defined with const are the same for all class objects, so you need to access const-defined constants as you would access static members, and a mutation error can be accessed using the members of the object. In addition, access to static constants is compiled by replacing constants with the values of constants, such as:
int nvalue = Max_value;
After compiling this sentence, it is the same as the intermediate language code produced by the following sentence.
int nvalue = 10;
However, there are many limitations on types when defining constants with Const. First, this type must belong to a value type, and the initialization of this type cannot be done through new, so some value-type constants defined with struct cannot be defined with Const.
In contrast to const, it is much more flexible to define constants with ReadOnly, and it is written in the following ways:
public readonly int max_value = 10;
Why is it called a dynamic variable because the system allocates space for the constants defined by ReadOnly, that is, the same as the other members of the class. In addition, the constants defined by ReadOnly can be set in the constructor of a class in addition to the constant values that are defined. Because the constants defined by ReadOnly are equivalent to the members of the class, you use the const to define the type limits of the constants, which disappear when you use ReadOnly to define them, that is, you can define constants of any type with readonly. As described above, the difference between the two is specific as follows.
Static constants (Compile-time constant) dynamic constants (Runtime constant)
To set a constant value while defining a declaration. You can set a constant value without having to set it in a class constructor.
Type limits the first type must belong to a range of value types, and its value cannot be set by new. There is no limit, and you can use it to define constants of any type.
For class objects, constants have the same value for objects of all classes. For different objects of a class, the value of a constant can vary.
Memory consumption is not available. To allocate memory, save constant entities.
Summary performance is slightly high, no memory overhead, but a lot of constraints, inflexible. Flexible, convenient, but with a slightly lower performance and memory overhead.
As for defining constants, whether it is defined by const or READONLY, I used to define it as a const, in order to pursue performance. But in this book, there is a reference to using const to create a potential bug. When using a static constant of a class in a DLL class library in a program, if the value of a static constant is modified in the class library, the other interfaces do not change, and in general, the program caller does not need to recompile, and the new class library can be invoked directly by execution. In this case, however, a potential bug is generated. This is because the static constant, when compiled, replaces the constant with its value, so the program on the caller side replaces it. For example, a static constant is defined in the class library, as follows:
public const int max_value = 10;
The code that calls this static constant in the program, in the intermediate language code that is compiled, is replaced with 10来, where the static constants are used, to 10. So when the static variables of the class library change, for example:
public const int max_value = 15;
Then the caller-side program can be run without recompiling, but the program's intermediate language code corresponds to the value of a static variable of 10 instead of 15 in the new class library. As a result of this inconsistency, the program raises potential bugs. The solution to this type of problem is that the caller-side program compiles after the class library is updated, generating a new intermediate language code.
The potential bugs that exist when you define constants as described above do not occur when you define constants with ReadOnly. Because readonly-defined constants are similar to members of a class, they need to be accessed based on specific constant addresses to avoid such bugs.
In view of this, this book suggests using readonly to replace const to define constants.
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.