Confusing warning about a constant decimal field in C#

前端 未结 1 1158
情书的邮戳
情书的邮戳 2021-01-17 15:39

I was experimenting with the const modifier while exploring a plethora of C# tutorials, and placed a bunch of const modifiers in a class like this

1条回答
  •  离开以前
    2021-01-17 16:38

    Int is a simple value type of a fixed size. Decimal is a bit more complicated due to scale. If you decompile your code, you'll find that it looks like this:

    [DecimalConstant(0, 0, 0, 0, 1)]
    private readonly static decimal somedecimal;
    
    private const int someint = 2;
    

    Where the decimal is not a constant, but has a DecimalConstant attribute courtesy of mscorlib.dll, where the true definition if decimal is:

    public struct Decimal : IFormattable, IComparable, IConvertible,
    IDeserializationCallback, IComparable, IEquatable
    

    Much more in-depth exploration of this topic is covered in this blog post.

    0 讨论(0)
提交回复
热议问题