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 int
s but I'm not sure.
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?
It is a base type, it's part of the specifications of the language, but not a primitive.
In JavaScript, a primitive (primitive value, primitive data type) is data that is not an object and has no methods or properties. There are 7 primitive data types: string. number.
Approach 1: In this approach, we check the type of the value using the typeof operator. If the type of the value is 'object' or 'function' then the value is not primitive otherwise the value is primitive. But the typeof operator shows the null to be an “object” but actually, the null is a Primitive.
Primitive data types are number, string, boolean, NULL, Infinity and symbol.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With