Why is decimal not a primitive type?

前端 未结 3 449
自闭症患者
自闭症患者 2020-12-11 00:34

Why is decimal not a primitive type?

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

outputs false.

It is

3条回答
  •  情深已故
    2020-12-11 01:22

    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

    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.

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

提交回复
热议问题