Hi Friends
Can anybody differenciate between the term Read-only and Constant?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Karthik AgarwalRank #2,270 · 620 reputationPosted Dec 5, 2011, 7:47 AM EST
http://blogs.msdn.com/b/csharpfaq/archive/2004/12/03/274791.aspx
VulpesRank #55 · 29.2K reputationPosted Dec 5, 2011, 7:53 AM EST
Vineet Kumar SainiRank #257 · 9.8K reputationPosted Dec 5, 2011, 7:59 AM EST
Const – Compile-Time Constant Values
Readonly - Runtime Constant References
The difference is that the value of readonly field is set at run time, so it can have a different value for different executions of the program. However, the value of a const field is set to a compile time constant.
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.
When using const, the value has to be set before compiling, and can not be changed.
when using readonly, the value is set when the instance is created. This means that you can specify the value of the readonly in the constructor of your class.
Another difference between const and readonly, is that const are static by default, where with readonly, you must define it as static (if you want it to be).
Thanks.....