6

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?

I Stand With Israel
  • 1,971
  • 16
  • 30
user1713153
  • 219
  • 5
  • 12
  • 1
    See http://msdn.microsoft.com/en-us/library/aa664635(v=vs.71).aspx and http://msdn.microsoft.com/en-us/library/aa711900(v=vs.71).aspx – Tim S. Nov 20 '12 at 00:10
  • @TimS. One of your links is for C#, the other for VB.NET. – svick Nov 20 '12 at 00:11
  • I believe they are the same. – gdoron Nov 20 '12 at 00:16
  • 2
    No, they are not. This question has been asked here before - for particular types ([String](http://stackoverflow.com/questions/3965752/is-string-a-primitive-type)) and [in general](http://stackoverflow.com/questions/2106132/why-do-primitive-types-in-c-sharp-have-their-own-operations). While there's obvious room for interpretation of terminology, predefined types are built-ins and primitive are another name of value types. – raina77ow Nov 20 '12 at 00:18
  • https://stackoverflow.com/questions/62503/should-i-use-int-or-int32 – phougatv Oct 02 '21 at 03:53

2 Answers2

11

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.

raina77ow
  • 103,633
  • 15
  • 192
  • 229
6

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.

myroslav
  • 1,670
  • 1
  • 19
  • 40
  • This isn't an accurate example, for example: `int a = 10; int b = (int) -a; int c = (System.Int32) -a;` in that example `b` is fine, but `c` will output an error. Sometimes the framework type and its alias don't mean the same. – Héctor Álvarez Mar 14 '18 at 12:12
  • Thanks for the feedback Hector. Your example will not work because you will need to enclose -a in parentheses. You are getting the error at compile time because as compiler says: "To cast a negative value, you must enclose the value in parentheses ". It is still only syntactic difference as I said above. I can make your example work like so: int a = 10; int b = int.Parse((-a).ToString()); int c = System.Int32.Parse((-a).ToString()); It is still a very interesting observation though and I wonder why does compiler treat this example like that. – myroslav Mar 14 '18 at 15:39
  • 3
    This article describes this situation: https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0075 – myroslav Mar 14 '18 at 15:50
  • 2
    @HéctorÁlvarez - `(System.Int32) -a` isn't valid, but `-(System.Int32)a` is. Both `-(int)a` and `(int)-a` are valid, though. – Bobson Jun 28 '18 at 18:37