Hi
What is the difference between const and readonly in C#.Net?
Difference between const and readonly in C#
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
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
VulpesRank #55 · 29.2K reputationPosted Jul 20, 2012, 2:27 AM EDT
In contrast, the 'readonly' modifier can only be applied to a static or instance field - it can't be applied to a local variable. Also, it can only be given a value either where it's defined or within the enclosing type's constructor and cannot be changed after that. However, it's value is not determined until runtime and can therefore be of any type, not just the built-in types.
Santhosh Kumar JayaramanRank #563 · 4.3K reputationPosted Jul 20, 2012, 2:27 AM EDT
- Can't be static.
- Value is evaluated at compile time.
- Initiailized at declaration only.
'readonly':