const


public const string MyStr;
C# const is a compile time constant. It must be initialized and initialization must be at compile time . A const string can only be initialized inline . A value cannot be changed from anywhere (constructor, function, runtime, etc. nowhere). When you use a const string in C#, the compiler embeds the string's value at compile time . Therefore, if you use a const value in a different assembly, then update the original assembly and change the value, the other assembly won't see the change until you again compile it.

Difference between readonly and const keyword in C#

static readonly


public static readonly string MyStr;
C# static readonly is runtime constant and it can use a default value, without initializing. Initialization can be done at run time . It means that it is evaluated when the application is launched and not before. A static readonly string can be set in a static constructor, not through a member function. A static readonly string is a normal field that gets looked up at runtime . Therefore, if the field's value is changed in a different assembly, the changes will be seen as soon as the assembly is loaded, without recompiling . A static readonly string can use non-constant members, such as Environment.UserName or DateTime.Now.ToString() .
Prev
Index Page
Next
©Net-informations.com