const vs. static readonly

s5s picture s5s · Nov 27, 2011 · Viewed 8.2k times · Source

Possible Duplicate:
What is the difference between const and readonly?

So from what I read, in C#, const and static readonly will both make a value unalterable during the execution of a program.

However, const should be used with quantities which are unlikely to ever change (e.g. pi, radius of earth, litters per gallon etc.).

On the other hand, static readonly should be used with values that currently are constant but might/will change in the future (e.g. software version, a multiplier in an algorithm etc.).

Have I got it right?

Answer

Ry- picture Ry- · Nov 27, 2011

I don't know about your second item (I would probably use a constant for a software version or an algorithm… constant) but there is one key difference between the two: const can only hold basic types such as string, bool, or numeric types. static readonly can hold any object. So, for example, I often use static readonly to store resources like Bitmap objects. Those cannot be const.