There are two types of constants in C #, ReadOnly (run-time) and const (compile-time), and this article compares the different characteristics of the two types and describes their respective scenarios.
Working principle
ReadOnly is a run-time constant, which is assigned when the program is run, and cannot be changed after the assignment is completed, so some people call it a read-only variable.
Const is a compile-time constant, and when the program compiles, it parses the const value and replaces all constant references with the corresponding values.
The following is a declaration of two constants:
public static readonly int A = 2; A is a run-time constant
public const int B = 3; b is a compile-time constant
The following expression:
int C = A + B;
After compiling is equivalent to the following form:
int C = A + 3;
As you can see, the const constant B is replaced with the literal 3, and ReadOnly constant a keeps the reference.
Declaration and initialization
ReadOnly constants can only be declared as class fields, support instance types or static types, be initialized at the same time as declared or initialized in constructors, and cannot be changed after initialization is complete.
A const constant, in addition to being declared as a class field, can also be declared as a local constant in a method, which defaults to a static type (which does not require a static adornment, or causes a compilation error), but must be initialized at the same time as the declaration.
Data type Support
Because const constants are replaced with literal literals at compile time, their value types are limited by a certain amount. 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 as a reference, and after a constant is updated, all references to the constant are given the updated value.
The const situation is slightly more complicated, especially across assembly calls:
public class Class1
{
public static readonly int A = 2; A is a run-time constant
public const int B = 3; b is a compile-time constant
}
public class Class2
{
public static int C = class1.a + class1.b; The value of the variable C is the sum of a, b
}
Console.WriteLine (CLASS2.C); Output "5"
Assuming that Class1 and Class2 are located in two different assemblies, now change the constant values in Class1:
public class Class1
{
public static readonly int A = 4; A is a run-time constant
public const int B = 5; b is a compile-time constant
}
Compile Class1 and deploy (note: Class2 is not recompiled at this point) and look at the value of variable C again:
Console.WriteLine (CLASS2.C); Output "7"
The result may be a bit unexpected, so let's take a closer look at the assignment expression for variable C:
public static int C = class1.a + class1.b;
After compilation is equivalent to the following form:
public static int C = class1.a + 3;
Therefore, no matter how the value of the constant b changes, it will have no effect on the final result. Although recompiling the CLASS2 can solve this problem, at least let us see the maintenance problem that the const can bring.
Performance comparison
Const directly participates in the computation in the literal form, the performance is slightly higher than ReadOnly, but for the general application, this kind of performance difference can be said to be negligible.
Applicable scenarios
In the following two scenarios:
A. The value is permanent (e.g. pi, number of hours per day, radius of the earth, etc.)
B. Very demanding program performance requirements
You can use const constants, and in other cases, you should take precedence over ReadOnly constants.
Two types of constants-readonly and const