我最近试图回答用户发布的关于为什么decimal结构const不像其他所有数字原语一样声明其Min/Max值的问题; 相反,Microsoft文档声明它是static readonly.
在研究中,我挖掘了微软的源代码,并提出了一个有趣的发现; 源代码(.NET 4.5)使它看起来const与文档明确指出的内容相反(源代码和相关的结构构造函数粘贴在下面).
public const Decimal MinValue = new Decimal(-1, -1, -1, true, (byte) 0);
public const Decimal MaxValue = new Decimal(-1, -1, -1, false, (byte) 0);
public Decimal(int lo, int mid, int hi, bool isNegative, byte scale)
{
if ((int) scale > 28)
throw new ArgumentOutOfRangeException("scale", Environment.GetResourceString("ArgumentOutOfRange_DecimalScale"));
this.lo = lo;
this.mid = mid;
this.hi = hi;
this.flags = (int) scale << 16;
if (!isNegative)
return;
this.flags |= …Run Code Online (Sandbox Code Playgroud)