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.
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.
Pramod GehlotPosted May 6, 2015, 5:01 AM
Nanhe SiddiquePosted May 6, 2015, 4:22 AM
http://www.dotnet-tricks.com/Tutorial/csharp/FU4N141113-Difference-Between-Constant-and-ReadOnly-and-Static.html
Manoj BhoirPosted May 6, 2015, 3:50 AM
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