Comparison of two constants in C #-const, readonly

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 declaration can be understood as follows (Note: The following statements are incorrect and may cause compilation errors.).

Public static const int max_value = 10;

Constants defined by const are the same for all class objects,Therefore, you need to accessConstDefinition of constants, and access by object members will 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 or string type, and the initialization of this type cannot passNewTo complete, so some use structThe defined value type constant cannot use const either.To define.

 

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 mustReadonlyThe defined constant allocation space, that is, it has the same independent space as other members of the class.. In addition to defining constant values,You can also set it in the class constructor.. 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)

Definition

You must set the constant value when declaring.

You do not need to set constant values when declaring them. You can set them in the constructor of the class.

Type restrictions

First, the type must 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

For all class objects, the constant value is the same.

For different objects of a class, the constant value can be different.

Memory consumption

None.

To allocate memory, save the constant entity.

Summary

The performance is slightly higher and there is no memory overhead, but there are many restrictions and they are 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.