Interviews3 answers
What is the difference between Static, Const and read only?
3 Answers
static means the variable is common to all
const means
if we initialize variable first time then never be changed
we have to initialize at the time of declearation.
& readonly is changed one time in constructor.
thanks
Mukesh KumarPosted Aug 22, 2017, 6:16 PM EDT
The readonly keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used.
Navdeep SinghPosted Mar 30, 2017, 1:05 AM EDT
Const: Const is nothing but "constant", a variable of which the value is constant but at compile time. And it's mandatory to assign a value to it. By default a const is static and we cannot change the value of a const variable throughout the entire program.
Readonly: Readonly is the keyword whose value we can change during runtime or we can assign it at run time but only through the non-static constructor. Not even a method
Static ReadOnly: A Static Readonly type variable's value can be assigned at runtime or assigned at compile time and changed at runtime. But this variable's value can only be changed in the static constructor. And cannot be changed further. It can change only once at runtime
Gnanavel SekarPosted Feb 19, 2017, 9:26 PM EST



