user1713153
user1713153

Reputation: 219

C# predefined types vs primitive types?

I have been studying C# lately and there is a thing called "predefined type". I thought it is another name of the primitive type. But my friend told me that those are quite different from each other. Just got confused.

Are these two names for the same thing, or are they totally different?

Upvotes: 6

Views: 3739

Answers (2)

myroslav
myroslav

Reputation: 1778

Predefined are the types that are specially supported by the compiler. They alias .NET framework types in the System namespace. They are also called built-in types.

How are they supported by the compiler? They have their own keyword. For example int keyword is used to predefine System.Int32 type. This means that when you define an integer variable in C#, you can do so by typing:

int myIntVar = 3;

instead of

System.Int32 myIntVar = 3;

This is only syntatic difference though.

Primitive types are all predefined value types minus decimal type. I made a sketch that demonstrates this:

enter image description here

Hope this helps.

Upvotes: 6

raina77ow
raina77ow

Reputation: 106375

In the Type.IsPrimitive documentation page there's a complete list of primitive types:

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

And section 1.2.1 (Predefined types) clearly makes the difference between these and predefined reference types:

The predefined reference types are object and string. The type object is the ultimate base type of all other types. The type string is used to represent Unicode string values.

So I guess it's quite obvious they are different - at least, in .NET terminology.

Upvotes: 11

Related Questions