In C #, a const is defined as a constant, that is, the value (and not the calculation that created it!) Is directly embedded in the compiled assembly.
So that you do not do something problematic, the compiler does not allow you to assign the result of the calculation to a constant. To understand why, imagine that this is possible:
A.dll public enum Foo { A, B } B.dll public const NumberOfFoos = Enum.GetNames(typeof(Foo)).Length;
Now you change A.dll:
A.dll public enum Foo { A, B, C, D } B.dll public const NumberOfFoos = 2;
Therefore, you will need to recompile B.dll to get the correct value.
Deteriorating: Imagine you had a C.dll assembly that uses B.NumberOfFoos. The value 2 will be directly embedded in C.dll, so it will also need to be recompiled after B.dll to get the correct value. Therefore (pit of success), C # does not allow you to fulfill this assignment in const.
source share