C # Two implementation methods for defining Constants

Source: Internet
Author: User

There are two ways to define constants in C #: Compile-time constant and Runtime constant ). The former is defined by "const", and the latter is defined by "readonly. For a static constant (Compile-time constant), the writing method is as follows:
Public const int MAX_VALUE = 10;
Why is it a static constant, because the above statement can be understood as follows (Note: The following statements are incorrect and may cause compilation errors, which is just for convenience ).
Public static const int MAX_VALUE = 10;
Constants defined by const are the same for all class objects. Therefore, you need to access the constants defined by const as you access static members, however, access by object members may cause a mutation error. In addition, access to static constants replaces constants with their values during compilation. For example:
Int nValue = MAX_VALUE;
After compilation, this sentence is the same as the intermediate language code generated in the following sentence.
Int nValue = 10;
However, there are many restrictions on the type when using const to define constants. First, this type must belong to the value type, and initialization of this type cannot be completed through new. Therefore, some value type constants defined by struct cannot be defined by const.
Compared with const, it is more flexible to use readonly to define constants. The writing method is as follows:
Public readonly int MAX_VALUE = 10;
Why is it a dynamic variable, because the system allocates space for constants defined by readonly, that is, it has independent space like other members of the class. In addition to defining constant values, the constants defined by readonly can be set in the constructor of the class. Since the constants defined by readonly are equivalent to the members of the class, the type restriction for defining constants using const disappears when readonly is used for definition, you can use readonly to define constants of any type. In summary, the differences between the two are as follows.

Static constant (Compile-time constant) Dynamic constant (Runtime constant)
When defining a declaration, you must set a constant value. You do not need to set constant values when declaring them. You can set them in the constructor of the class.
The Type restriction must first belong to the value type range, and its value cannot be set through new. No limit. You can use it to define constants of any type.
For class objects, constant values are the same for all class objects. For different objects of a class, the constant value can be different.
No memory consumption. To allocate memory, save the constant entity.
Summary performance is slightly higher, no memory overhead, but there are many restrictions, not flexible. Flexible and convenient, but with low performance and memory overhead.

When defining constants, whether it is defined by const or readonly is used. In the past, I tried to use const to define constants in pursuit of performance. However, this book mentions a potential bug about using const. When a program uses a static constant of a class in the DLL class library, if the value of the static constant is modified in the class library, other interfaces do not change. Generally, the new class library can be called directly by executing the program without re-compiling. However, in this case, potential bugs may occur. This is because the static constant is replaced with its value during compilation, so is the replacement of the program at the call end. For example, a static constant is defined in the class library, as follows:
Public const int MAX_VALUE = 10;
For the code that calls this static constant in the program, in the compiled intermediate language code, it is replaced with 10, that is, the place where the static constant is used is changed to 10. When the static variables of the class library change, for example:
Public const int MAX_VALUE = 15;
Therefore, the calling end program can be run without re-Compiling. However, the intermediate language code of the program corresponds to the value of the static variable 10, rather than 15 in the new class library. Therefore, such inconsistencies may cause potential bugs. The method to solve this problem is to re-compile the calling program after updating the class library to generate a new intermediate language code.

For the above potential bugs that exist when the const defines constants, readonly does not occur when defining constants. Because the constants defined by readonly are similar to the members of the class, you need to access them according to the specific constant address during access to avoid such bugs.
In this case, we recommend that you replace the const with readonly to define constants.

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.