Even though you have been writing C # for many yearsCodeBut when someone asks about the difference between your const and readonly, it may take a while ~
I am also reading the. NETProgramIn the interview book, I found that I had been confused about the two for a long time. Indeed, const and readonly are similar. They declare the variables as read-only and cannot be rewritten after the variables are initialized. So what are the differences between the const and readonly modifiers? In fact, this involves two different constant types in C #: compile-time constants and runtime constants ). The two have different characteristics. Incorrect use will not only result in loss of efficiency, but also cause errors.
First, explain what is a static constant and what is a dynamic constant. A static constant is a constant that is parsed by the compiler during compilation and replaced with the initial value. The value of a dynamic constant is obtained at the moment of running. during compilation, it is marked as a read-only constant instead of a constant value, in this way, Dynamic Constants do not need to be initialized during declaration, but can be delayed to initialization in the constructor.
When you have a general understanding of the two concepts above, you can describe const and readonly. The constant modified by const is the first one in the preceding section, that is, the static constant, and readonly is the second one, that is, the dynamic constant. The differences can be illustrated by the characteristics of static constants and Dynamic Constants:
1) const-modified constants must be initialized during Declaration; readonly-modified constants can be delayed until the constructor is initialized.
2) const-modified constants are parsed during compilation, that is, the constant value is replaced with the initial value. readonly-modified constants are delayed until running.
In addition, the const constant can be declared in the class or in the function body, but the static readonly constant can only be declared in the class.
Some beginners may be confused by the above purely conceptual explanation. The following are some examples:
Using System;
Class P
{
Static Readonly Int A = B * 10 ;
Static Readonly Int B = 10 ;
Public Static Void Main ( String [] ARGs)
{
Console. writeline ( " A is {0}, B is {1} " , A, B );
}
}
What is the output result of the above Code? Many people think that A is 100 and B is 10! In fact, the correct output result is a is 0 and B is 10. Well, if it is changed to the following:
Using System;
Class P
{
Const Int A = B * 10 ;
Const Int B = 10 ;
Public Static Void Main ( String [] ARGs)
{
Console. writeline ( " A is {0}, B is {1} " , A, B );
}
}
What is the output result of the above Code? Is it a is 0, B is 10? In fact, it is wrong again. The correct output result is a is 100 and B is 10.
So why? As mentioned above, const is a static constant, so the values of A and B are determined during compilation (that is, when the value of B is 10, and a = B * 10 = 10*10 = 100), the output in the main function is of course a is 100, B is 10. Static readonly is a dynamic constant. The variable value is not parsed during compilation. Therefore, it is the default value at the beginning. For example, if both A and B are of the int type, they are all 0. When the program runs to a = B * 10; so a = 0*10 = 0, and the program then runs to B = 10, the true initial value 10 of B is assigned to B. If you still don't know clearly, we can use the ildasm tool provided by Microsoft to open it by entering ildasm under vs 2008 Command, as shown below:
Open the executable files generated after the above two codes are compiled, as shown in:
Static readonly executable program structureConst executable program structure
The constants A and B are displayed in the preceding two figures. Double-click the node to see the difference:
STAtic readonly modifier constant a const modifier constant
STAtic readonly modifier constant B const modifier constant B
It can be seen that the const modified constant has calculated the nominal values of A and B during compilation, while the static readonly modified constant is not parsed, therefore, the main function has the following differences:
STMain function of the atic readonly program const Program Main Function
From the main function, we can see that the output of the const program is 100 and 10, while the readonly program does P: A and P: B in the output, it is determined that the values of constants A and B are delayed until they are run, so the output is 0 and 10.
So what are the characteristics of Static and Dynamic Constants? In fact, static constants can only be declared as simple data types (INT and floating-point type), enumeration, Boolean, or string type, while Dynamic Constants can also modify some object types. For example, the datetime type is as follows:
// Error
Const datetime time = new datetime ();
// Correct
Static readonly datetime time = new datetime ();
The above error is that you cannot use the new keyword to initialize a static constant, even if it is a value type, because new will cause the value to be determined at runtime, which is contrary to the fixed literal value at the time of static variable compilation.
The comparison between static constants and Dynamic Constants is shown in the following table: