On Sep 29, 8:21*pm, puzzlecracker <ironsel2...@gmail.comwrote:
On Sep 29, 11:46*am, "Peter Morris" <mrpmorri...@SPAMgmail.comwrote:
const is set at compile time, readonly is set within the constructor.
Any reason to use readonly then?
Versioning. When you make a library, you should only use non-private/
internal const if you can guarantee that the value won't change in the
future versions of the library. The reason is simple: when an
application references your library, and its code uses the constant,
the value of that constant is substituted directly into the compiled
assembly. Even if you later change the library to a newer one, the
compiled client code will still use the old value of the constant.
With readonly, it is not the case - it is a proper variable, and its
value is actually read at run-time when the assembly is loaded, so if
it changes in the assembly, the change will be seen by all clients on
the next run.
In general, when in doubt, prefer readonly. The only thing you really
lose is the ability to use it as a case label in a switch statement;
but you will certainly not introduce silent breakage, as is possible
with const and separate builds.