Decimal.MinValue & Decimal.MaxValue: why static readonly and not const modifiers?

constants, numeric, readonly, static, types

Solution

Although MSDN library describes decimal `MinValue` field as being `static readonly`, C# compiler treats it as `const`.

If `MinValue` field was readonly, following code would not compile, but it actually does compile.

`const decimal test = decimal.MinValue - decimal.MinValue;`

Remarks:

① Same answer applies to decimal type `MaxValue` field.

② For further details, Jon Skeet's gives here an insight on how constant field implementation at IL level differs between:

- Primitive numeric types (integrals, float and double) and

- The non primitive numeric decimal type.

Problem

In C#, MinValue field is defined for numeric types with: ① static readonly modifiers for decimal type (Link to MSDN Libray for .NET 4.5): ``` public static readonly decimal MinValue ``` ② const modifier for all other numeric types: ``` //Integral signed numeric types public const sbyte MinValue public const short MinValue public const int MinValue public const long MinValue //Integral unsigned numeric types public const byte MinValue public const ushort MinValue public const uint MinValue public const ulong MinValue //Real numeric types public const float MinValue public const double MinValue ``` Why const modifier is not used to define Decimal.MinValue field ? Remarks: ① Same question applies to numeric types MaxValue field. ② VB, C++ and F# also use different modifiers for decimal type so this question is not specific to C# language.

Original source

Related problems