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;
}
}
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;
}
}
0 comments:
Post a Comment
Note: only a member of this blog may post a comment.