/* by Dylan SUN */
Today let us talk about const and readonly.
- Const is considered as Compile-time constant
- ReadOnly is considered as runtime constant.
I'll demonstrate some example code to clarify their usages and differences.
I. Const usages
Firstly, you can see that the const variable can is declared and assigned at class level.
publicclass ConstExample{ publicconststring"hello"//Correct}
You can also declare a const variable in constructor
publicConstExample(){ conststring"hehe"//Correct}
You can see that const can is declared at method level too
publicvoidDisplay(){ conststring"cst 2"//Correct : Constant can be declared in method}
If you want to assign another value to CstValue1, you'll get an compile-time exception. Because Const can only is assigned when declaration.
CstValue1"hello2"//Compile:CannotCstValue1
You can not also assign another value to CstValue1 in the constructor. You'll get an compile-time exception.
publicConstExample(){ "hello2"//Compile time exception : Constant can not be used as an assignment target}
May is want to assign a normal string variable to a const like this. But you'll see it doesn ' t work too.
publicstring v1;conststring//Compile time exception : Constant initializer must be compile-time constant
But you can assign the operation result of several consts to another a const like this.
publicconststring"world"//Correctpublicconststring//Correct : Constants can only be assigned with values or with consts
You can also declare a const, and assign it with the addition of the same consts in the method.
conststring cstValue5 = CstValue1 + CstValue3;
When your display them you can see
//=> hello//=> cst 2//=> helloworld//=> helloworld
II. ReadOnly usages
Firstly, you can declare a readonly variable at class level.
publicclass ReadOnlyExample{ publicreadonlystring"good";}
You can also just declare a readonly variable without assigning any value to it.
publicreadonlystring rdValue2;
You can be declare a readonly variable in a method.
readonlystring"hohoho"//compile time exception : statement expected
You cannot assign a value to variable rdValue1 after the declaration, except for, you assign a value in the class Construc Tor.
"good 2"//Compile:Cannot resolve symbol rdValue1
You can not assign a readonly variable to another readonly variable at class level.
stringtimeaccess‘rdValue1incontext
You can be assign a normal variable to a readonly variable neither at class level.
string"hey"stringtimeaccess‘value1incontext
You can be assign value to a readonly variable in a method.
publicvoidDisplay(){ "good one"//Compile time exception: Readonly field can not be used as an assignment target "good too"//Compile time exception: Readonly field can not be used as an assignment target}
But you can assign a normal variable to a readonly variable in class constructor.
You can even assign the operation result of several readonly variables to another readonly variable.
publicreadonlystring rdValue4;publicreadonlystring rdValue5;publicReadOnlyExample(stringvalue){ value//Correct //Correct : Assign another readonly variable to another readonly variable can only be done in constructor //Correct : Assign readonly variable with normal variable to another readonly variable}
When your display them, you'll see
//=> good//=> day//=> goodday//=> goodhey
If you want to access to a const, you must access it via the class
Console.WriteLine(ConstExample.CstValue1//Constclass
If you want to access to a readonly variable, you must access it via an instance of class.
varnew ReadOnlyExample("day"//byofclass
So to conclude,
Const:
Can only is assigned at declaration
Can is declared at class level, in constructor and in method.
Can be assigned with operation result of several consts at class
Level, or in constructor, or in method. (like addition, multiplication
etc
Once the declaration is do, you can never modify a const ' s value, neither in constructor, nor just after declaration.
You can access the const variable only by class.
Readonly:
Can only is assigned at declaration or in constructor.
Can is declared only at class level. You can declare a readonly
Variable neither in constructor nor in method.
Can be assigned with operation result of several readonly variables only in constructor (like addition, multiplication etc ). You can not assign them to a readonly variable when declaration or in a method.
You can access readonlyvariable only by the instance of class.
I Hope you find this article helpful!
Just4fun-comparaison between Const and ReadOnly in C #