Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# predefined types vs primitive types?

Tags:

c#

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?

like image 765
user1713153 Avatar asked Nov 20 '12 00:11

user1713153


2 Answers

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.

like image 143
raina77ow Avatar answered Sep 27 '22 20:09

raina77ow


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.

like image 22
myroslav Avatar answered Sep 27 '22 21:09

myroslav