What is the difference between const and static read-only member?
A const field must be initialized at the place where it is declared as shown in the example below.
class Program
{
public const int Number = 100;
}
It is a compile time error to declare a const without a value. The code below will generate a compiler error stating "A const field requires a value to be provided"
class Program
{
public const int Number;
}
It is a compile time error to change the value of a constant. The following code will generate a compiler error stating "The left-hand side of an assignment must be a variable, property or indexer"
class Program
{
public const int Number = 100;
static void Main()
{
Number = 200;
}
}
It is not mandatory to initialize a static readonly field where it is declared. You can declare a static readonly field without an initial value and can later initialize the static field in a static constructor as shown below.
class Program
{
public static readonly int Number;
static Program()
{
Number = 100;
}
}
Once a static readonly field is initialized, the value cannot be changed. The code below will generate a compiler error stating "A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)"
class Program
{
public static readonly int Number;
static Program()
{
Number = 100;
}
static void Main()
{
Number = 200;
}
}
In short, the difference is that static readonly field can be modified by the containing class, but const field can never be modified and must be initialized where it is declared. A static readonly field can be changed by the containing class using static constructor as shown below.
class Program
{
// Initialize the static readonly field
// to an initial value of 100
public static readonly int Number=100;
static Program()
{
//Value changed to 200 in the static constructor
Number = 200;
}
}
Subscribe to:
Post Comments (Atom)
if you use the member of a class from another assembly there is a difference. The value of const member will be placed in the class and there is no refference to the actual field. So if you change it later the change will not affect all the compiled assemblies which used that constant. Readonly fields save the reference and as soon as you change the value that value will be change in all other assemblies.
ReplyDeleteExplanation is right, but correct answer is, The value of a static readonly field is set at runtime; therefore, the value can be modified by the containing class. On the other hand, the value of a const field is set to a compile-time constant.
ReplyDeleteCan a static read only field get changed dynamically via a call to the static constructor, like this:
ReplyDeletestatic Program(int number)
{
Number = number;
}
Can a static read only field be changed dynamically in the static constructor like this:
ReplyDeletestatic Program(int number)
{
Number = number;
}
Hi Andy,
ReplyDeleteA static constructor is always parameterless and is automatically invoked by CLR. So above code is not valid to initialize static readonly field.