Const variable in C# are compile time variable i.e value is known at compile time.
The assignment to const variable is only done at the time of declaration and this value of variable remain constant throughout lifetime of the program.
These variable are accessed via "Classname.Variablename".
Such variable can be declared and intialized in any method of class.
Readonly keyword
Readonly is a keyword which when used with a variable then the value of that variable can only be changed either at runtime or at the time of the instance initialization.
The assignment to readonly variable is only done at the time of declaration or through constructor of the class.
These variable are accessed via "Instancename.Variablename".
Readonly keyword cannot be used with variables in the methods even the value of readonly variable cannot be changed in any method of the class.
0 Comment(s)