Valid tive C # Two Methods for defining Constants

Source: Internet
Author: User

InC #There are two ways to define constants. One is called a static constant (Compile-time constant), The other is a dynamic constant (Runtime constant). The former uses"Const"To define, the latter uses"Readonly.

 

For static constants (Compile-time constant). The writing method is as follows:

Public Const IntMax_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 IntMax_value = 10;

UseConstDefined constants 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:

IntNvalue = max_value;

After compilation, this sentence and the intermediate language produced by the following sentenceCodeIs the same.

IntNvalue = 10;

 

HoweverConstWhen defining constants, there are many restrictions on the type. First,This type must belong to the value type or string type, and the initialization of this type cannot passNewThereforeStructThe defined value type constant cannot be used either.ConstTo define.

 

RelativeConstFor exampleReadonlyIt is much more flexible to define constants. The writing method is as follows:

Public Readonly IntMax_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,ReadonlyIn addition to defining constant values,You can also set it in the class constructor.. BecauseReadonlyThe defined constant is equivalent to a member of the class, soConstTo define the type restrictions of constants.ReadonlyAll disappears when you define them, that is, you can useReadonlyTo define constants of any type.

 

In summary, the differences between the two are as follows.

 

Static constants (Compile-time constant)

Dynamic Constants ( runtime constant )

definition

you must set constant values when declaring them.

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 passNew.

no restrictions, 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 values can be different.

memory consumption

none.

memory to be allocated, stores constant entities.

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 constantsConstTo define orReadonlyIn the past, I tried to useConst. But in this book, I mentionedConstWill generate potentialBug. InProgramUsed inDLLWhen a static constant of a class in the class library is modified in the class library, other interfaces do not change. In general, the program call end does not need to be re-compiled, you can call the new class library directly. However, in this case, the potentialBug. 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 IntMax_value = 10;

Therefore, for the code that calls this static constant in the program, it is used in the compiled intermediate language code.10To replace the static constant10.

When the static variables of the class library change, for example:

Public Const IntMax_value = 15;

Therefore, the caller program can be run without re-Compiling. However, the intermediate language code of the program corresponds to the value of the static variable.10Instead15. Therefore, such inconsistencies may lead to potentialBug. 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.

 

ForConstPotential for defining ConstantsBug, In useReadonlyIt does not occur when a constant is defined. BecauseReadonlyThe defined constants are similar to the members of the class. Therefore, you must access them according to the specific constant address to avoid suchBug.

 

In view of this, it is recommended to use this bookReadonlyTo replaceConstTo define constants.

Related Article

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.