Method for defining constants in C #

Source: Internet
Author: User

In C #, the # define Preprocessor instruction cannot be used to define constants, which is usually used in C and C ++. This requires that all references to constants use the class name as the prefix, as shown in the following example.

Static class Constants

{

Public const double Pi = 3.14159;

Public const int speedoflight = 300000; // km per sec.

 

}

Class Program

{

Static void main ()

{

Double Radius = 5.3;

Double area = constants. Pi * (radius * radius );

Int secsfromsun = 149476000/constants. speedoflight; // in km

}

}

 

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.

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.