const vs readonly
-
Consts must be asigned at compilation; this means that every time you run the programe they have the same value. Read only can be asigned a value when they are declared and they can't be changed afterwards; this works "readonly int j=i" where i is a variable. So j can have different values the programe is run;
-
Hi, The difference is -- The value of const is evaluated at compile time and the value of readonly are evaluated at runtime. Means u cannot assign value to const at runtime like this --
const int a = someVariable;
but you can do --readonly int a = someVariable;
"A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder
Anant Y. Kulkarni
-
Hi, The difference is -- The value of const is evaluated at compile time and the value of readonly are evaluated at runtime. Means u cannot assign value to const at runtime like this --
const int a = someVariable;
but you can do --readonly int a = someVariable;
"A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder
Anant Y. Kulkarni
Hello Anant,
Anant Y. Kulkarni wrote:
const int a = someVariable;
'someVariable' or must have some constant value? if i changed value of someVariable before upper statement then it will be runtime binding. Isn't it?
regards, Divyang Mithaiwala System Engineer & Software Developer
-
Hello Anant,
Anant Y. Kulkarni wrote:
const int a = someVariable;
'someVariable' or must have some constant value? if i changed value of someVariable before upper statement then it will be runtime binding. Isn't it?
regards, Divyang Mithaiwala System Engineer & Software Developer
Hi,
The difference is --The value of const is evaluated at compile time and the value of readonly are evaluated at runtime.
**Means u cannot assign value to const at runtime like this --const int a = someVariable;**
but you can do --
readonly int a = someVariable;
I meant that you cannot assign a variable to a const. It should be a constant like -- const int a= 50 I just wrote the opposite.
"A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder
Anant Y. Kulkarni
-
You also keep in mind the limitations of const vs. read-only. In the case that you have an assembly you distribute, internally or to customers, that contains CONST values. Since CONSTs are set at compile time, if you update your values and distribute the updated assembly unless the apps are recompiled with your new assembly they will still have the old vlaues for the CONST variables.
only two letters away from being an asset
-
Taken from the MSDN[^] documentation for the readonly keyword:
When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class. 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. Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants.