When you use the string const , the compiler enters the string value at compile time.
Therefore, if you use the value of const in another assembly, update the original assembly and change the value, the other assembly will not see the change until you recompile it.
A static readonly string is a normal field that is viewed at runtime. Therefore, if the field value changes in another assembly, the changes will be visible immediately after loading the assembly without recompiling.
It also means that the static readonly string can use mutable elements like Environment.UserName or DateTime.Now.ToString() . The string const can only be initialized using other constants or literals.
Alternatively, the static readonly string can be set in the static constructor; The string const can only be initialized inside the string.
Note that a static string can be changed; you should use static readonly .
SLaks Jul 06 '10 at 23:29 2010-07-06 23:29
source share