为什么十进制不是原始类型?

时间:2022-01-22 16:08:56

Why is decimal not a primitive type?

为什么十进制不是原始类型?

Console.WriteLine(typeof(decimal).IsPrimitive);

outputs 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 decimal in the framework? An int for example has a field m_value of type int. A double has a field m_value of type double. It's not the case for decimal. It seems to be represented by a bunch of ints but I'm not sure.

它是基本类型,是语言规范的一部分,而不是原语。在框架中,什么原始类型表示小数?例如,一个int类型的字段m_value,一个double类型的字段m_value。不是十进制的情况。它似乎是由一堆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?

为什么它看起来像一个原始类型,它的行为像一个原始类型(除了几个例子之外),但是它不是一个原始类型?

3 个解决方案

#1


20  

Although not a direct answer, the documentation for IsPrimitive lists 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:

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 decimal is 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).

对我来说,似乎十进制是一种类型必须存在语言/运行时想要CLS CLI-compliant(,因此称为“原始的”,因为它是一个关键字的基本类型支持),但实际的实现不需要真正的“原始”(如CLR并不认为这是一个原始数据类型)。

#2


9  

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.

Decimal是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).

在十进制的情况下,它只是一个内部使用整数的复合数据类型,因此它的性能比与计算机内存直接相关的数据类型(ints, double等等)要慢。

#3


0  

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,float all being a primitive type takes single instruction to perform the assignemnt operation whereas decimal,string being non primitive types takes more than one native instruction to perform this operation.

正如您可以看到的,float都是一个原始类型,它只接受单个指令来执行赋值操作,而decimal,string是非基元类型,需要多个本机指令来执行此操作。

#1


20  

Although not a direct answer, the documentation for IsPrimitive lists 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:

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 decimal is 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).

对我来说,似乎十进制是一种类型必须存在语言/运行时想要CLS CLI-compliant(,因此称为“原始的”,因为它是一个关键字的基本类型支持),但实际的实现不需要真正的“原始”(如CLR并不认为这是一个原始数据类型)。

#2


9  

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.

Decimal是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).

在十进制的情况下,它只是一个内部使用整数的复合数据类型,因此它的性能比与计算机内存直接相关的数据类型(ints, double等等)要慢。

#3


0  

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,float all being a primitive type takes single instruction to perform the assignemnt operation whereas decimal,string being non primitive types takes more than one native instruction to perform this operation.

正如您可以看到的,float都是一个原始类型,它只接受单个指令来执行赋值操作,而decimal,string是非基元类型,需要多个本机指令来执行此操作。