Difference between constant and static readonly
difference between constant and static readonly in c#
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
Manoj BhoirRank #599 · 4K reputationPosted May 5, 2015, 11:50 PM EDT
Constants are declared using a "const" keyword.
Constants should be assigned a value at the time of the variable declaration and hence are known at compile time. Now whenever you declare a constant variable, the C# compiler substitutes its value directly into the Intermediate Language (MSIL).
Readonly :
Readonly variables are a little different from their colleague, const.
Readonly variables are known at runtime, they can be assigned a value either at runtime or at the time of the instance initialization. Again, lets understand through code here.
Constants:
1. Constants can be assigned values only at the time of declaration
2. Constant variables have to be accessed using "Classname.VariableName"
3. Constants are known at compile time
Read Only:
1. Read only variables can be assigned values either at runtime or at the time of instance initialization via constructor
2. Read only variables have to be accessed using the "InstanceName.VariableName"
3. Read only variables are known at run time.
For more info check these links :
Practical Difference between Const & ReadOnly
http://www.codeproject.com/Tips/803656/Practical-Difference-between-Const-ReadOnly
Difference Between Constant and ReadOnly and Static
http://www.dotnet-tricks.com/Tutorial/csharp/FU4N141113-Difference-Between-Constant-and-ReadOnly-and-Static.html
Nanhe SiddiqueRank #1,103 · 1.7K reputationPosted May 6, 2015, 12:22 AM EDT
http://www.dotnet-tricks.com/Tutorial/csharp/FU4N141113-Difference-Between-Constant-and-ReadOnly-and-Static.html
Pramod GehlotRank #2,471 · 568 reputationPosted May 6, 2015, 1:01 AM EDT