If you're going to possibly be changing it, you should use readonly
here. const
should really be used for things that will never change, like π. The reason for this is because of a subtle difference between const
and readonly
.
The main issue is that if you change the value of the const
, you must recompile all dependent clients that use the const
, otherwise you can shoot yourself in the foot, badly. So for values that might change, don't use const
, use readonly
.
So, if the value is never going to change, just use const
and then don't worry about defining Epsilon
in terms of ROUND
, just say:
internal const int ROUND = 3;
public const double Epsilon = 1e-3;
If you really want to make sure you don't accidentally change one without changing the other, you could add a small check in the constructor:
if (Epsilon != Math.Pow(10, -ROUND)) {
throw new YouForgotToChangeBothConstVariablesException();
}
You could even add conditional compilation so that only gets compiled in debug releases.
If it is going to change, use static readonly
:
internal readonly int ROUND = 3;
public static readonly double Epsilon = Math.Pow(10, -ROUND);
With this limitation I see no way I can define both interdependant constants as consts. [...] Am I missing an obvious way of how to do this?
No, you need to do some kind of math using Math.Pow
or Math.Log
to go between ROUND
and Epsilon
and those are not acceptable for compile-time usage with const
. You could write a miniature code generator to spit out these two lines of code based on a single input value, but I really question the value of investing time into that.