const keyword can be applied to fields or local variables
We must assign const field at time of declaration
No Memory Allocated Because const value is embedded in IL code itself after compilation.
It is like find all occurrences of const variable and replace by its value.
So the IL code after compilation will have hard-coded values in place of const variables
Const in C# are by default static.
The value is constant for all objects
There is dll versioning issue - This means that whenever we change a public const variable or property , (In fact, it is not supposed to be changed theoretically), any other dll or assembly which uses this variable has to be re-built
Only C# built-in types can be declared as constant
Const field can not be passed as ref or out parameter
ReadOnly
readonly keyword applies only to fields not local variables
We can assign readonly field at the time of declaration or in constructor,not in any other methods.
dynamic memory allocated for readonly fields and we can get the value at run time.
Readonly belongs to the object created so accessed through only through instance of class. To make it class member we need to add static keyword before readonly.
The value may be different depending upon constructor used (as it belongs to object of the class)
If you declare a non-primitive types (reference type) as readonly only reference is immutable not the object it contains.
AsSince the value is obtained at run time, there is no dll versioning problem with readonly fields/ properties.
We can pass readonly field as ref or out parameters in the constructor context.
CONST
const keyword can be applied to fields or local variables
We must assign const field at time of declaration
No Memory Allocated Because const value is embedded in IL code itself after compilation.
It is like find all occurrences of const variable and replace by its value.
So the IL code after compilation will have hard-coded values in place of const variables
Const in C# are by default static.
The value is constant for all objects
There is dll versioning issue - This means that whenever we change a public const variable or property , (In fact, it is not supposed to be changed theoretically), any other dll or assembly which uses this variable has to be re-built
Only C# built-in types can be declared as constant
Const field can not be passed as ref or out parameter
ReadOnly
readonly keyword applies only to fields not local variables
We can assign readonly field at the time of declaration or in constructor,not in any other methods.
dynamic memory allocated for readonly fields and we can get the value at run time.
Readonly belongs to the object created so accessed through only through instance of class. To make it class member we need to add static keyword before readonly.
The value may be different depending upon constructor used (as it belongs to object of the class)
If you declare a non-primitive types (reference type) as readonly only reference is immutable not the object it contains.
As the value obtained at run time there is no dll versioning problem with readonly fields/ properties
We can pass readonly field as ref or out parameters in the constructor context.
CONST
const keyword can be applied to fields or local variables
We must assign const field at time of declaration
No Memory Allocated Because const value is embedded in IL code itself after compilation.
It is like find all occurrences of const variable and replace by its value.
So the IL code after compilation will have hard-coded values in place of const variables
Const in C# are by default static.
The value is constant for all objects
There is dll versioning issue - This means that whenever we change a public const variable or property , (In fact, it is not supposed to be changed theoretically), any other dll or assembly which uses this variable has to be re-built
Only C# built-in types can be declared as constant
Const field can not be passed as ref or out parameter
ReadOnly
readonly keyword applies only to fields not local variables
We can assign readonly field at the time of declaration or in constructor,not in any other methods.
dynamic memory allocated for readonly fields and we can get the value at run time.
Readonly belongs to the object created so accessed through only instance of class. To make it class member we need to add static keyword before readonly.
The value may be different depending upon constructor used (as it belongs to object of the class)
If you declare a non-primitive types (reference type) as readonly only reference is immutable not the object it contains.
Since the value is obtained at run time, there is no dll versioning problem with readonly fields/ properties.
We can pass readonly field as ref or out parameters in the constructor context.
const keyword can be applied to fields or local variables
We must assign const field at time of declaration
No Memory Allocated Because const value is embedded in IL code itself after compilation.
It is like find all occurrences of const variable and replace by its value.
So the IL code after compilation will have hard-coded values in place of const variables
Const in C# are by default static.
The value is constant for all objects
There is dll versioning issue - This means that whenever we change a public const variable or property , (In fact, it is not supposed to be changed theoretically), any other dll or assembly which uses this variable has to be re-built
Only C# built-in types can be declared as constant
Const field can not be passed as ref or out parameter
ReadOnly
readonly keyword applies only to fields not local variables
We can assign readonly field at the time of declaration or in constructor,not in any other methods.
dynamic memory allocated for readonly fields and we can get the value at run time.
Readonly belongs to the object created so accessed through only through instance of class. To make it class member we need to add static keyword before readonly.
The value may be different depending upon constructor used (as it belongs to object of the class)
If you declare a non-primitive types (reference type) as readonly only reference is immutable not the object it contains.
As the value obtained at run time there is no dll versioning problem with readonly fields/ properties
We can pass readonly field as ref or out parameters in the constructor context.