beginner's question: const vs readonly

R

R.A.M.

Hello,
I am learning C#.NET and I donst understand difference between meaning
and usage of "readonly" and "const" keywords. Could you explain me
please.
Thank you
/RAM/
 
M

Marc Gravell

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);}
}
}
 
M

Mark Wilden

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).

Interesting. (And you mean readonly in the last sentence, I assume.)

///ark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top