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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Vineet Kumar SainiPosted Dec 5, 2011, 12:59 PM
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.....
VulpesPosted Dec 5, 2011, 12:53 PM
Karthik AgarwalPosted Dec 5, 2011, 12:47 PM
http://blogs.msdn.com/b/csharpfaq/archive/2004/12/03/274791.aspx