Readonly and const in C,
Compare readonly and const in C #
C # has two constant types: readonly (runtime volume) [read-run] and const (compilation volume) [const-compile]. this article compares the two types of features and describes their application scenarios.
Working Principle
Readonly is the runtime constant. It is assigned a value when the program is running. It cannot be changed after the value is assigned. Therefore, some people call it a read-only variable.
Const is the compile-time constant. During program compilation, constant values are parsed and all constant references are replaced with corresponding values.
The following two constants are declared:
Public static readonly int A = 2; // A indicates the running time.
Public const int B = 3; // B indicates the compile time.
The following expression:
Int C = A + B;
After compilation, it is equivalent to the following form:
Int C = A + 3;
As you can see, const constant B is replaced with the word surface volume 3, while readonly constant A remains the reference mode.
Declaration and initialization
The readonly constant can only be declared as a class field. It supports instance type or static type. It can be initialized at the same time of Declaration or in the constructor. After initialization, it cannot be changed.
In addition to being declared as a class field, const constants can also be declared as local constants in methods. The default value is static type (without static modification; otherwise, compilation errors will occur ), however, initialization must be completed at the same time as the declaration.
Supported Data Types
Const constants are replaced with literal values during compilation, which limits their value types. Const constants can only be assigned numbers (integers, floating-point numbers), strings, and enumeration types. The following code cannot be compiled:
Public const DateTime D = DateTime. MinValue;
Change to readonly to compile normally:
Public readonly DateTime D = DateTime. MinValue;
Maintainability
Readonly works in reference mode. After a constant is updated, all the places that reference this constant can get the updated value.
Const is a little more complex, especially for cross-assembly calls:
Public class Class1
{
Public static readonly int A = 2; // A indicates the running time.
Public const int B = 3; // B indicates the compile time.
}
Public class Class2
{
Public static int C = Class1.A + Class1. B; // The value of variable C is the sum of A and B.
}
Console. WriteLine (Class2.C); // output "5"
Assume that Class1 and Class2 are in two different sets, and now change the constant value in Class1:
Public class Class1
{
Public static readonly int A = 4; // A indicates the running time.
Public const int B = 5; // B indicates the compile time.
}
Compile and deploy Class1 (Note: Class2 is not re-compiled at this time), and check the value of variable C again:
Console. WriteLine (Class2.C); // output "7"
The result may be a little unexpected. Let's take a closer look at the value assignment expression of variable C:
Public static int C = Class1.A + Class1. B;
After compilation, it is equivalent to the following form:
Public static int C = Class1.A + 3;
Therefore, no matter how the value of constant B changes, it will not affect the final result. Although re-compiling Class2 can solve this problem, at least we can see the possible maintenance problems caused by const.
Performance Comparison
Const is directly involved in the operation in literal form, and the performance is slightly higher than readonly, but for general applications, this performance difference can be said to be minimal.
Applicable scenarios
In the following two cases:
A. The value remains unchanged permanently (such as the circumference rate, the number of hours in a day, and the Earth's radius)
B. Strict Program Performance Requirements
You can use the const constant. In other cases, the readonly constant should be used first.
In the C language, what is the symbol (->) and how to use it?
This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.
In the C language, what is the symbol (->) and how to use it?
This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.