Last post Jan 02, 2012 09:10 PM by Paul Linton
Jan 02, 2012 12:41 PM|Rahul_agg11|LINK
Can anyone tell me why Integer is a Value type while String is a reference type.
Jan 02, 2012 12:50 PM|karthicks|LINK
all .net datatypes has default size except string and user type. so string is reference type, bcos its does not have default allocation size
Jan 02, 2012 01:02 PM|Careed|LINK
An integer has fixed memory set at 4 bytes. Hence, an integer can be directly stored within fixed memory, or on the stack.
Alternatively, a string does not have a pre-defined memory size, so it requires dynamic memory. When a string object is created, the actual value is stored within dynamic memory, or on the heap. To access it, a reference to this memory space is stored
in the stack, thus the name "reference type".
Note that when you create an object variable, a value-type variable contains the actual value whereas a reference-type variable contains only the reference to the actual location of the object's value.
Jan 02, 2012 01:05 PM|Rahul_agg11|LINK
Thanks for detail explanation.
Jan 02, 2012 07:11 PM|lamado|LINK
a string is some how an array of char,
you must know that a string variable have special exeption when dealing with it that makes it act like a value type.
Jan 02, 2012 09:10 PM|Paul Linton|LINK
The types of the C# language are divided into two main categories: Value types and reference types. (C# Language Specification Ch4)
The previous replies are correct. Lamado, however, hints at something important. 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).
This difference becomes real when you use equality tests.
int x = 3;
int y = 3;
// x==y is true
Color front = Color.AliceBlue;
Color back = Color.AliceBlue;
// front == back is true
Employee sweeper = new Employee("John", "Smith");
Employee ceo = new Employee("John", "Smith");
// sweeper == ceo is false
string name = "John";
string facility = "John";
// name == facility is true
strings have value semantics, they behave like normal value types such as int and Color. They do not behave like entities. Confusion arises because the common assumption is that a reference type behaves like an entity, but this is not the case for a string.
You may create your own reference types which have value semantics, just like string (if you fo this with all of your types then you are on the road to inventing functional languages, have a look at F#).
Note the important statement - you cannot change a string. Strings are immutable.