const vs. static readonly
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 (eg 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 (eg software version, a multiplier in an algorithm etc.).
Have I got it right?
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
.
上一篇: 常量和只读在C#中?
下一篇: 常量与静态只读