Const expressions are evaluated at compile time for all assemblies using
them
Readonly expressions are evaluated at runtime either in the initializer
(readonly int 5 = {initializer};), or in the constructor; after the
constructor they cannot be changed. This can be useful to avoid accidental
updates to the field, or where the "const" expression can only be evaluated
at runtime - e.g. from a database of config file.
Note (more abstract) also that if assembly A references B, and B has a const
that A uses, then swapping B with an updated const value will *not* reflect
in A, as A's value is compiled already; with consts it *will* show sooner,
but it needs a runtime eval (once).
Also - you can have readonly fields at both the static and instance level,
but consts are (essentially) static. As an example of a readonly instance
field that varies per instance, consider the decorator / facade wrapper
pattern that provides alternative access to the members of an existing
object (e.g. the following that converts methods on the base into properties
on the decorator) - it rarely makes sense to change the base object of such
a wrapper, so it can be readonly.
public class SomeDecorator {
private readonly SomeOtherClass _base;
public SomeDecorator(SomeOtherClass base) {
_base = base;
}
public string SomeProperty {
get {return _base.SomeComplexMethod();}
set {_base.UpdateSomething(value);}
}
}