C # Constants

Source: Internet
Author: User

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 static const int max_value = 10;This is wrong, the compiler Error const constant cannot be static, it should be written as follows:
Public const int max_value = ten;
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 compile error is made by accessing the object's member way. 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.

static constant (compile-time constant) dynamic constant (Runtime

constant
Defined
A constant value is set at the same time as the declaration. When declaring, you can set the constant value without having to set it in the class.
Type restrictions
The first type must belong to a value type range, and its value cannot be set by new. There is no limit, and you can use it to define constants of any type.
For a class object, the value of the constant is the same for all classes of objects. For different objects of a class, the value of the constant can be not the same. Memory consumption is not. To save the constant entity.
Summary performance is slightly high, no memory overhead, but a lot of restrictions, inflexible. Flexible and convenient, but with slightly lower performance and memory overhead.
As for defining constants, whether they are defined by const or READONLY, I used to define them as const in order to pursue performance. But in this book,, a potential bug was 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 code generated after the compilation is replaced with 10来, where static constants are used, instead of 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 code of the program corresponds to the value of the static variable is 10, not the new class library of 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.

Reference from

C # 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.