Primitive Types & Value Types
Questions of the day
- Is there any difference between Primitive Types & Value Types ?
- Are all Value Types primitive types and vise versa ?
- Is there any difference between string & System.String ?
These questions are basically what we are going to discuss briefly today, because there is a big misunderstanding in this area.
so first of all, the answers of the previous questions are
- Yes, there is a difference between Primitive & Value types
- No, neither all value types are primitives nor the vise versa
- No, there is no difference between string & System.String
So let's consider this table
|
Value Types
|
Primitive Types
|
Basic Definition
| Value types directly contain their data, and instances of value types are either allocated on the stack or allocated inline in a structure. Value types can be built-in (implemented by the runtime), user-defined (structs), or enumerations (enums).
| Basic system types considered by the compiler as the basic building blocks for any other data structure
|
Examples
| - sbyte (System.SByte)*
- byte (System.Byte)
- short (System.Int16)
- ushort (System.UInt16)*
- int (System.Int32)
- uint (System.UInt32)*
- long (System.Int64)
- ulong (Sysem.UInt64)*
- char (System.Char)
- float (System.Single)
- double (System.Double)
- bool (System.Boolean)
- decimal (System.Decimal)
- All variables defined from a struct data type
- All Enumerations (enum) types
| - sbyte (System.SByte)*
- byte (System.Byte)
- short (System.Int16)
- ushort (System.UInt16)*
- int (System.Int32)
- uint (System.UInt32)*
- long (System.Int64)
- ulong (Sysem.UInt64)*
- char (System.Char)
- float (System.Single)
- double (System.Double)
- bool (System.Boolean)
- decimal (System.Decimal)
- string (System.String)
- object (System.Object)
|
| * Indicates a Non-CLS compliant type |
As you can see from the previous table, that not all value types are listed in the primitive types, also you'll notice that primitive types contain two
Reference Types (string & object), which is why not all primitive types are value types.
So why there is such distinction between Value Types & Primitive Types ..
If you have a look at every type in last table you'll find that every Value type corresponds to a
System.<Something> CLR type, and also it inherits from System.ValueType, so originally if you want to declare a variable of type
int, you should write it in this way
System.Int32 i = new System.Int32();
and this is the standard declaration / initialization format for any type in CTS. However, as a syntactic point of view, this is really silly and you'll find it weird to write such line for every variable especially if these variables are used frequently,
and this is exactly the main reason of Primitive types, the primitive types are a shortcut for declaring such variables so instead of the previous line and also making them familiar with the language itself, like int in C# and Integer in VB, you'll just write this
int i;
and this will be completely equivalent to the previous line.
so from here we can understand why
string &
object types are considered primitive types, because other wise, you would have to write this for every string variable you declare
System.String s = new System.String()
which is silly because we use String all the time.
However, the compiler will also accept the following declaration which is more convenient than the first declaration
Int32 i;
So, you don't need to write the part after the new keyword.
I hope you find this useful, good luck