What is the difference between const and read only fields?
Const is
evaluated at compile time, readonly is evaluated at runtime. const must be
initialized at declaration time only where as readonly field can be initialized
in the declaration or constructor.
Const must be used very carefully
especially when you try to make them public. Compiler will do something like
find-and-replace for consts. So if a 3rd party assembly takes a dependency on
your assembly that exposes const as public, then the const value will be there
in 3rd party assebly's IL. So, if you change the value of the const later, then
the 3rd party assembly has to be re-compiled (and possibly re-packaged and
re-shipped).
No comments:
Post a Comment