vb.net 为什么十进制不是原始类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13471941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is decimal not a primitive type?
提问by Guillaume
Why is decimalnot a primitive type?
为什么decimal不是原始类型?
Console.WriteLine(typeof(decimal).IsPrimitive);
outputs false.
输出false。
It is a base type, it's part of the specifications of the language, but not a primitive. What primitive type(s) do represent a decimalin the framework? An intfor example has a field m_valueof type int. A doublehas a field m_valueof type double. It's not the case for decimal. It seems to be represented by a bunch of ints but I'm not sure.
它是一种基本类型,是语言规范的一部分,但不是原始类型。哪些原始类型decimal在框架中代表 a ?一个int例如具有场m_value型的int。Adouble有一个m_value类型的字段double。情况并非如此decimal。它似乎由一堆ints表示,但我不确定。
Why does it look like a primitive type, behaves like a primitive type(except in a couple of cases) but is not a primitive type?
为什么它看起来像一个基本类型,表现得像一个基本类型(除了少数情况)但不是一个基本类型?
回答by Adam Houldsworth
Although not a direct answer, the documentation for IsPrimitivelists what the primitive types are:
虽然不是直接的答案,但文档IsPrimitive列出了原始类型是什么:
http://msdn.microsoft.com/en-us/library/system.type.isprimitive.aspx
http://msdn.microsoft.com/en-us/library/system.type.isprimitive.aspx
A similar question was asked here:
在这里问了一个类似的问题:
http://bytes.com/topic/c-sharp/answers/233001-typeof-decimal-isprimitive-false-bug-feature
http://bytes.com/topic/c-sharp/answers/233001-typeof-decimal-isprimitive-false-bug-feature
Answer quoted from 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.
CLR 不需要任何关于十进制类型的内在知识——它将它视为另一种恰好具有重载运算符的值类型。例如,没有直接对小数进行运算的 IL 指令。
To me, it seems as though decimalis a type that must exist for a language/runtime wanting to be CLS/CLI-compliant (and is hence termed "primitive" because it is a base type with keyword support), but the actual implementation does not require it to be truly "primitive" (as in the CLR doesn't think it is a primitive data type).
在我看来,decimal对于想要符合 CLS/CLI 的语言/运行时来说,似乎是一种必须存在的类型(因此被称为“原始”,因为它是具有关键字支持的基本类型),但实际的实现确实如此不需要它是真正的“原始”(因为在 CLR 中不认为它是原始数据类型)。
回答by Valentin Vasilyev
Decimal is a 128 bit data type, which can not be represented natively on a computer hardware. For example a 64-bit computer architecture generally has integer and addressing registers that are 64 bits wide, allowing direct support for 64-bit data types and addresses.
十进制是一种 128 位数据类型,无法在计算机硬件上本地表示。例如,64 位计算机体系结构通常具有 64 位宽的整数和寻址寄存器,允许直接支持 64 位数据类型和地址。
Wikipedia says that
维基百科说
Depending on the language and its implementation, primitive data types may or may not have a one-to-one correspondence with objects in the computer's memory. However, one usually expects operations on basic primitive data types to be the fastest language constructs there are.
根据语言及其实现,原始数据类型可能与计算机内存中的对象一一对应,也可能不一一对应。然而,人们通常期望对基本原始数据类型的操作是最快的语言结构。
In case of decimal it is just a composite datatype which utilizes integers internally, so its performance is slower than of datatypes that have a direct correlation to computer memory (ints, doubles etc).
在十进制的情况下,它只是一种内部使用整数的复合数据类型,因此其性能比与计算机内存(整数、双精度等)直接相关的数据类型慢。
回答by Hameed Syed
Consider the below example ,
考虑下面的例子,
int i = 5;
float f = 1.3f;
decimal d = 10;
If you put a debugger and verify the native instruction set,it would be
如果您放置调试器并验证本机指令集,它将是
As you can see int,floatall being a primitive type takes single instruction to perform the assignemnt operation whereas decimal,stringbeing non primitive types takes more than one native instruction to perform this operation.
如您所见,int、float都是原始类型需要单条指令来执行赋值操作,而decimal、string是非原始类型需要多条本机指令来执行此操作。


