为什么是小数不是原始类型?小数、原始、类型、不是

2023-09-04 02:38:05 作者:- 真心我用过

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

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

输出

有碱型,它的语言的规范的一部分,但不是原始的。哪些基本类型(S)做重新present的框架十进制?一个 INT 例如有一个字段 m_value 类型 INT 。 A 有型 m_value 字段。它不是为十进制的情况。这似乎是重新$ P $由一群 INT 的psented小号,但我不知道。

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.

为什么它看起来像一个基本类型,就像一个原始人键入的(除了在一个couple例),但不是原始类型?

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?

推荐答案

虽然没有直接回答,对于文档 IsPrimitive 列出什么样的基本类型有:

Although not a direct answer, the documentation for IsPrimitive lists what the primitive types are:

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

回答乔恩斯基特报价:

的CLR不需要有大约十进制任何内在知识   键入 - 它把它就像恰好有另一个值类型   重载运算符。有没有直接操作IL指令   小数位上,例如。

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.

对我来说,它好像十进制是,必须存在一个语言/运行时想成为CLS / CLI标准的类型(并因此被称为原始因为它与关键字支持基础型),但实际执行情况并不需要它是真正的原始人(如CLR不认为这是一个基本数据类型)。

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