readonly
Facts
1) It’s a modifier which is assigned while field declarations.
2) Also called run time constant.
3) Value can be assigned in 2 ways:
a. During declaration
//Initializing value during declaration
readonly int Readonly = 5;
b. By constructor
class ReadOnlyDemoClass
{
//Declaration
readonly int Readonly;
//Initializing value by constructor
ReadOnlyDemoClass(int x)
{
Readonly = x;
}
}
4) IL
readonly Properties
1) Only get; method should be declared and defined in readonly properties
2) Value can be assigned in 2 ways:
a. During declaration
//Variable Declaration and initialization
private int readonlyvar = 5;
//Readonly Property Declaration - No set method
internal int ReadonlyProperty { get { return readonlyvar; }}
b. By constructor
//ReadOnlyDemo Class
class ReadOnlyPropertyDemoClass
{
//Variable Declaration and initialization
private int readonlyvar = 5;
//Readonly Property Declaration - No set method
internal int ReadonlyProperty { get { return readonlyvar; }}
//Constructor
public ReadOnlyPropertyDemoClass(int propVal)
{
//Initializing value by constructor
readonlyvar = propVal;
}
}
//Program Class
class Program
{
//Main Method
static void Main(string[] args)
{
ReadOnlyPropertyDemoClass rpdc = new ReadOnlyPropertyDemoClass(5);
Console.WriteLine(rpdc.ReadonlyProperty);
}
}
3) IL
4) Static readonly properties can be assigned only during declaration or by static constructor
5) Static readonly properties should be associated only with static variables.
Errors
1) Initializing the readonly variable by any different method.
class ReadOnlyDemoClass
{
readonly int Readonly;
static void Main(string[] args)
{
ReadOnlyDemoClass rd = new ReadOnlyDemoClass();
//Initializing the variable from somewhere else
rd.Readonly = 5; // Compilation Error: A readonly field cannot be assigned to (except in a constructor or a variable initializer)
}
}
Resolution: Use the above two mentioned ways to initialize the readonly variable.
2) Assigning Static readonly variable by normal constructor
//ReadOnlyDemo Class
class ReadOnlyDemoClass
{
//Static readonly field
static readonly int Readonly;
//Constructor
ReadOnlyDemoClass(int x)
{
//Initializing value by constructor
Readonly = x; // Compilation Error: A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)
}
//Main Method
static void Main(string[] args)
{
ReadOnlyDemoClass rd = new ReadOnlyDemoClass(4);
}
}
Resolution: Initialize the static readonly variable whilst declaration or using static constructor
Additional References
const
Facts
1) It’s a modifier which is assigned while field declarations.
2) Also called compile time constant.
3) Value can be assigned only during declaration:
a. During declaration
//Initializing value during declaration
const int constantvar = 5;
4) IL
5) As you can see in IL that const by itself is a static literal therefore, static modifier is not allowed for constants.
6) A constant can be used in an expression:
//Circle Class
class Circle
{
//Variable Declaration and initialization
const double pie = 22/7;
//Area Method
double Area(double radius)
{
//Use of constant in an expression
return 2 * pie * radius;
}
}
7) Expression containing constants:
int ConstAssignedVariable = Constant; // Valid Expression
const int ConstAssignedConstant = Constant; // Valid Expression
const int CalculatedwithConstConstant = Constant + 3; //Valid Expression
const int VarAssignedConstant = Variable; // Compilation Error: The expression being assigned to 'VarAssignedConstant' must be constant
const int CalculatedwithVariableConstant = Variable + Constant; // Compilation Error: The expression being assigned to 'CalculatedwithVariableConstant' must be constant
Errors
1) Variable assignment:
//Variable Declaration and initialization
const string constantvar = DateTime.Now.ToShortDateString();// Compilation Error: The expression being assigned to 'constantvar' must be constant
Resolution: Either assign the values which can be calculated at runtime or some constants.
2) Static Constants
static const int Constant = 2; // Compilation Error: The constant 'Constant' cannot be marked static
Resolution: Constants are implicitly static therefore, no need to mark them as static
Additional References
Difference between readonly and const