Figure 1 differences between the compile-time and runtime Constants
Compilation times are a little faster than running times, but it lacks flexibility.Program"Slow" can ensure correct operation, and "fast" may lead to errors, so I believe that we consider that "why do you care about the error between constants?" Of course, the first choice for stability is the runtime.
// Statement of compile time:
Public Const Int _ Millennium = 2000 ;
// Run-Time Volume statement:
Public Static Readonly Int _ Thisyear = 2011 ;
Figure 2 Runtime and compile-time msilCode
By comparing the msil code of the compilation constant and runtime constant, we can find that the compiled constant provides the constant value for the variable after compilation, while the runtime constant does not.
We know that the most important difference between runtime constant and compilation constant is that the analysis of runtime constant values occurs at runtime, while the analysis of compilation constant values occurs at compilation. In other words, the compiled il code often references the readonly variable rather than its value; when compiling, the compiled il code will directly reference its value, just as we use a constant value directly in the code. Even if we are using a numeric constant and cross-Assembly reference, the same is true: If we reference a constant in assembly B in assembly, after compilation, the constant in Assembly A will be replaced by its value. This difference has a considerable impact on code maintainability.
Now let's take a look at their differences:
/// <Summary>
/// Define runtime and compilation times
/// </Summary>
Public Class Testclass
{
Public Const Int Isconstvar = 5 ;
Public Static Readonly Int Inreadonlyvar = 1 ;
}
/// <Summary>
/// Differences between test run time and compile time
/// </Summary>
Class Program
{
Static Void Main ( String [] ARGs)
{
For ( Int I = Testclass. testclass. inreadonlyvar; I < Testclass. testclass. isconstvar; I ++ )
{
Console. writeline ( " This is {0} " , I );
}
Console. readkey ();
}
}
Figure 3 output result
Figure 4 compile constants and run constants msil
Through the above, we can find that the compile-time constant is replaced by a value constant by hard code, while the run-time constant is referenced by a reference method. If we modify the compile-time constant value to 10, the runtime constant value is 3, which is modified as follows:
Public Class Testclass
{
Public Const Int Isconstvar = 10 ;
Public Static Readonly Int Inreadonlyvar = 3 ;
}
If we re-compile testclass, but do not re-compile the program console application, and copy the re-compiled testclass to the bin of the console application to run it again, you can guess the final output result. I believe that you can guess the result. The correct result is as follows:
Figure 5 output result
The cause is that the C # compiler replaces isconstvar with its constant value 5 when compiling the console application for the first time. For inreadonlyvar, because it is declared as readonly, therefore, its discrimination occurs at runtime. Therefore, the console application assembly can still use the new inreadonlyvar value without being re-compiled.