A constant is a symbol that represents a constant value. When defining a constant, we must determine its value at compilation. When using a constant, the compiler first looks for the symbol from the module metadata that defines the constant, directly retrieves the value of the constant, and then embeds it into the compiled IlCode. Since the constant value is directly embedded in the Code, the constant does not need any memory allocation during runtime, so we cannot get the constant address, or a constant is passed as a reference.
In the following example, we will find that constants may cause version update problems:
Using System;
Public Class Component
{
Public Const Int32 maxentriesinlist = 50 ;
}
Reference thisProgramSet:
Using System;
Class App
{
Static Void Main ()
{
Console. writeline ("Max entries supported In List :" + Component. maxentriesinlist );
}
}
The above program references the maxentriesinlist constant. When the application is compiled, the compiler will find that maxentriesinlist is a constant symbol with a value of 50, the integer 50 is directly embedded into the application's il code. This will cause the application to run normally even if the referenced DLL assembly does not exist after compilation. we can delete component. dll after compilation is successful.
This means that if we change the maxentriesinlist in the previous component assembly to 1000, and re-compile maxentriesinlist. after DlLL, the app will not perceive all of this, because it will not go to maxentriesinlist again. DLL to obtain the value of the maxentriesinlist constant, unless we re-compile the app, this is a problem that must be noted and understood when using constants.