为什么 decimal
不是原始类型?
Console.WriteLine(typeof(decimal).IsPrimitive);
输出false
。
它是一种基本类型,是语言规范的一部分,但不是原始类型。哪些基本类型在框架中表示 decimal
?例如,int
有一个类型为 int
的字段 m_value
。 double
有一个类型为 double
的字段 m_value
。 decimal
不是这种情况。它似乎由一堆 int
表示,但我不确定。
为什么它看起来像原始类型,behaves like a primitive type (在 couple of cases 中除外)但不是原始类型?
最佳答案
虽然不是直接的答案,IsPrimitive
的文档列出了基本类型是什么:
http://msdn.microsoft.com/en-us/library/system.type.isprimitive.aspx
这里问了一个类似的问题:
http://bytes.com/topic/c-sharp/answers/233001-typeof-decimal-isprimitive-false-bug-feature
引用自 Jon Skeet 的回答:
The CLR doesn't need to have any intrinsic knowledge about the decimal type - it treats it just as another value type which happens to have overloaded operators. There are no IL instructions to operate directly on decimals, for instance.
对我来说,似乎 decimal
是一种必须存在于希望符合 CLS/CLI 的语言/运行时的类型(因此被称为“原始”,因为它是一个基础带有关键字支持的类型),但实际实现并不要求它是真正的“原始”(因为在 CLR 中并不认为它是原始数据类型)。
关于c# - 为什么 decimal 不是原始类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13471941/