The .NET expert tell me , how to differentiate the const and static read only member.
‘const’:
- Can’t be static.
- Value is evaluated at compile time.
- Initiailized at declaration only.
‘readonly’:
[list]
[] Can be either instance-level or static.
[] Value is evaluated at run time.
[*] Can be initialized in declaration or by code in the constructor
[/LIST].
Biggest operational thing is constants can be used in compile-time constructs, like attributes. Static readonly variables cannot.
major difference is scope.
static would be available for use through entire application.
whereas constant would be within the defining scope.
^^^ not really. Constants also must be attached to classes and they follow that scope. And they can be scoped – we use lots of private constants for magic numbers or strings inside classes.
If the above wasn’t enough, here’s a breakdown of all three components.
http://abdullin.com/journal/2008/10/22/const-vs-readonly-vs-static-readonly-in-c-net.html