Built-In Types in C# .NET … Value Type or Reference Type ??

30 Jan

We talked in the previous post about Variables, Constants and Data Types of each of them. You can see this post via this link.

Today we have to know what of the data types of the variables (Basic or Built-In Types) are a Value Type and what are a Reference Type ??

The answer in brief, all basic or built-in types are value types except strings.

The question that you will ask it is … Why strings ??

Strings aren’t value types since they can be huge, and need to be stored on the heap. Value types are (in all implementations of the CLR as of yet) stored on the stack. Stack allocating strings would break all sorts of things: the stack is only 1MB, you’d have to box each string, incurring a copy penalty, you couldn’t intern strings, and memory usage would balloon, etc…

The word ‘value‘ has another meaning and strings behave according to that other meaning. The other meaning relates to the difference between entities and values.  An entity is something which has identity but a value has no identity.  The number 3, for example, is the same as every other number 3, there is no sense in the concept of one 3 being different from another 3.  Furthermore you cannot change the number 3, it is always 3.  You may set a variable equal to a different number but there is no sense in which you change a number.

int x= 3;

x += 1;

This code did not change 3 into 4, it just changed x so that it has been given a new value.

Entities, however, have identity.  Two Employee entities called ‘John Smith’ are not the same, they have separate identity.  An Entity may change whilst retaining its identity.  An Employee may change its name but it is still the same Employee.  Strings are like values in this regard.  You cannot change a string.  You may set a variable equal to a different string but you never change the string (just like the int 3 is never changed)

When you have let’s say int value, Int type has defined lenght and that is 4 byte. And for every value type you know the exact memory space that will take. But for string you don’t know that. It will be stupid to have a string type in wich you should always define how long it is. And if you want to put in that string variable a value with bigger lenght that string is defined, that will be disallowed.

Why is string a special class. It is special because you have a reference class, but the way of using it, looks like a value type. So, that’s why you do operation like:

string C = A, and not string C = new string(A);

You don’t have to instance string class because .NET framework do that for you when you first time set it’s value. If you not set it’s value string has value of null like any other reference type. Every language platform has a special treatment for string type. .NET is not exclusion from that too..

Leave a comment

Posted by on January 30, 2012 in Visual Basic .NET, Visual C# . NET


Tags: , , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: