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.
Firstly, note that these
Decimal
values are seen as constants from the languages' point of view (but not the CLR), as Jon Skeet mentions in his answer to rmayer06's related question.(I thought the reason for using
ReadOnly
instead ofConst
was so that the constructor was not called on every use of aConst
, except that is :-( )If you compile:
x == Decimal.MaxValue
in C# or VB.NET, the constant is constructed as if it was really aConst
.In VB.NET this is not true for
Decimal.One
,Decimal.Zero
orDecimal.MinusOne
, but these are treated as constants in C#. (BTWString.Empty
is not seen as a constant in either language.)So I believe these are baked into to languages as constants (sometimes, in VB's case).
In VB's case it does seem to prefer to load the
ReadOnly
values, except forMaxValue
, andMinvalue
. (That isConst y1 As Decimal = Decimal.One
is effectively aliased by VB, but truly treated as a constant in C#.)BTW
Date.MaxValue
is alsoReadOnly
, but it is not seen as a constant by VB.NET (even though it does haveDate
literals).UPDATE: I haven't checked what the current results are for the most recent VB.NET or C# but the reference source does not treat any of
Decimal.One
,Decimal.Zero
,Decimal.MinusOne
,Decimal.MaxValue
orDecimal.MinValue
specially; they're all justpublic const
.I'd have thought it was for readability of the source code
seems more readable than the magic number:
Although MSDN library describes decimal
MinValue
field as beingstatic readonly
, C# compiler treats it asconst
.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:
This is an interesting question. I did some research, and the MS documentation on
Decimal
Min/Max fields explicitly says (note the wording is the same for both; shown in brackets for clarity):The following code compiles with no problem:
- Edit -
Note: here is the assignment of the field from the source for .NET 4.5 (downloaded PDB source from MS), the decimal constructor that is called by this code. Note that it is declaring a const value. It appears, at least for 4.5, that the documentation is wrong. (This would not be the first time MS documentation is incorrect). It also appears that the source code won't compile, as pointed out in a comment by @Daniel.
Also note: in the 2.0 framework, decimal is declared outright:
public const Decimal MaxValue = 79228162514264337593543950335m;
So, inconsistency and incorrect documentation is the conclusion I have reached. I will leave it to others to look at the other framework versions for a pattern.