Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is decimal not a primitive type?

Tags:

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.

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?

like image 601
Guillaume Avatar asked Nov 20 '12 11:11

Guillaume


People also ask

Is Decimal a primitive type?

It is a base type, it's part of the specifications of the language, but not a primitive.

What makes a type 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.

How do you know if a type is primitive?

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.

What are the 7 primitive data types?

Primitive data types are number, string, boolean, NULL, Infinity and symbol.


1 Answers

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

like image 137
Adam Houldsworth Avatar answered Oct 14 '22 19:10

Adam Houldsworth