3
votes

As you know, MSDN library says that string, decimal and object are not primitive types. I'm sharing a quote about this:

The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single.

We can test it also by typeof(typeName).IsPrimitive property. An addition, I read discussions about string type on stackoverflow.com too.

Nowadays I am reading a book as named CLR via C# (Fourth Edition) and this book says that (on page 112):

CLR via C#, Fourth Edition (Jeffrey Richter)

As you see, the writer(Jeffrey Richter) mentions that all these are primitive types. So, can anyone clarify me, what does he mean? Why these string, decimal, object, and dynamic types are in primitives list?

2

2 Answers

7
votes

I wouldn't personally call dynamic, decimal, object or string primitive types. I'd use Type.IsPrimitive for the canonical source there. Note that dynamic isn't even a type in the CLR sense.

The C# 5 MS specification only uses the word "primitive" twice - once in the introduction and once in quotes. The upcoming ECMA C# 5 standard doesn't use it at all. Given that the term "primitive" is well-defined for CLR types and isn't part of standardized terminology for C#, it seems like a bad idea to use it in this way. (There are places where the C# spec and the CLR disagree, e.g. on what conversions are available, whether structs have parameterless constructors etc. That's slightly different and more awkward.)

In standard C# terminology, there are simple types - but those are only the value types for which there are keywords.

I can't see any term in the C# 5 MS specification or the ECMA standard which describes "the keywords describing types".

1
votes

There are language primitive Torres and there are CLR primitive types. Language primitive types are types that are tested special by the compiler itself, like string and dynamic. CLR primitive types are the core building blocks of other types, like Boolean or Int32. The reflection function IsPrimitive returns true for CLR primitive types only, it doesn't know what programming language you used to write your code so it can't know about language primitive types. The only way to know which types are language primitive types is to read the language specification document. As pointed out, in C#, dynamic is a C# primitive type because the compiler tests it special (it allows special casting rules and dynamic member invocations with it). However, the compiler turns dynamic into Object at runtime. There is no dynamic type that the CLR knows about at all.