asp.net C # define constants and variables detailed

Source: Internet
Author: User
Tags constant

Constants also provide a convenient form of reading for these values, but unlike enumerations, constants are only a single value and, as their name shows, they are fixed and never changed. This is useful when you want to use a fixed value; Using constants means that you can name the value and use that name in your code. This value is defined once, so it is easy to modify, and the code becomes easier to maintain because it uses a readable name in your code.

Even if the value is used only once, it is advisable to use constants because they provide a way to centralize this type of fixed value together. The syntax for defining constants is as follows:

The code is as follows Copy Code

Private Const Constantname as Type=value

Const defines a value as a constant, and private refers to the visibility of the constant, which is described later in this chapter. The remainder of the declaration is the same as all other variables, except that the declared value is constant and therefore cannot be modified.

For example, in the Wrox United Web site, there is a shopping cart that stores the goods purchased from the store. Members of the club can enjoy a 10% discount, so the value can be saved as a constant:

The code is as follows Copy Code

Private Const Memberdiscountpercentage as single=0.1

After you have calculated the total price of an order, you can use the constant just as you would any other variable:

The code is as follows Copy Code

Memberdiscount=subtotal=memberdiscountpercentage
Total=subtotal-memberdiscount


There are two ways to define constants in C #, one called Static constants (Compile-time constant) and the other is called Dynamic constants (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:

The code is as follows Copy Code

public const int max_value = 10;

Why do you call it a static constant because, as the above declaration can be interpreted as follows (note: The following writing is wrong, it will be compiled incorrectly, just for the convenience of the description).

The code is as follows Copy Code

public static const int max_value = 10;

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 mutation error can be accessed using the members of the object. In addition, access to static constants is compiled by replacing constants with the values of constants, such as:

The code is as follows Copy Code

int nvalue = Max_value;

After compiling this sentence, it is the same as the intermediate language code produced by the following sentence.

The code is as follows Copy Code

int nvalue = 10;

However, there are many limitations on types when defining constants with Const. First, this type must belong to a value type, and the initialization of this type cannot be done through new, so some value-type constants defined with struct cannot be defined with Const.

In contrast to const, it is much more flexible to define constants with ReadOnly, and it is written in the following ways:

The code is as follows Copy Code

public readonly int max_value = 10;

Why is it called a dynamic variable because the system allocates space for the constants defined by ReadOnly, that is, the same as the other members of the class. In addition, the constants defined by ReadOnly can be set in the constructor of a class in addition to the constant values that are defined. Because the constants defined by ReadOnly are equivalent to the members of the class, you use the const to define the type limits of the constants, which disappear when you use ReadOnly to define them, that is, you can define constants of any type with readonly.

Static constants (Compile-time constant)
Dynamic constants (Runtime constant)

As for defining constants, whether it is defined by const or READONLY, I used to define it as a const, in order to pursue performance. But in this book, there is a reference to using const to create a potential bug. When using a static constant of a class in a DLL class library in a program, if the value of a static constant is modified in the class library, the other interfaces do not change, and in general, the program caller does not need to recompile, and the new class library can be invoked directly by execution. In this case, however, a potential bug is generated. This is because the static constant, when compiled, replaces the constant with its value, so the program on the caller side replaces it.

For example, a static constant is defined in the class library, as follows:

The code is as follows Copy Code
public const int max_value = 10;

The code that calls this static constant in the program, in the intermediate language code that is compiled, is replaced with 10来, where the static constants are used, to 10.

So when the static variables of the class library change, for example:

The code is as follows Copy Code

public const int max_value = 15;

Then the caller-side program can be run without recompiling, but the program's intermediate language code corresponds to the value of a static variable of 10 instead of 15 in the new class library. As a result of this inconsistency, the program raises potential bugs. The solution to this type of problem is that the caller-side program compiles after the class library is updated, generating a new intermediate language code.

The potential bugs that exist when you define constants as described above do not occur when you define constants with ReadOnly. Because readonly-defined constants are similar to members of a class, they need to be accessed based on specific constant addresses to avoid such bugs.
In view of this, this book suggests using readonly to replace const to define constants.


Summarize

A constant is the data whose value is fixed, and the value of the constant is determined at compile time. The type of a constant can only be one of the following types: sbyte, Byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, bool, and string.

C # uses the keyword const to declare constants, and when declaring constants, you must initialize them.

Variable, once a constant is defined, the value of the constant cannot be changed in the scope of the constant.

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.