The difference is pretty simple, but it is difficult to explain, particularly without pictures.
Fundamentally, when you deal with value type, you pass the actual object around, or you copy the actual object values. When you do the same with a reference type, you don't - you pass a reference to the object instead.
Take some sheets of paper. Each one represents a variable.
Value type:
Take two sheets. On the first one, label it "n1" and write a value - in your example zero.
On the second one, label it "n2" and copy the value from "n1"
Add one to the value on "n1"
Add two to the value on "n2"
Now take three other sheets. On the first one label it "n1". Get a second sheet, and label it "Numbers:instance1" - write a value on this sheet, in your example, zero.
On the sheet you labelled "n1" write the value "Numbers:instance1" This is a reference to the actual instance of the Numbers class.
On the third sheet, label it "n2" and copy the value from "n1". This will be the reference to "Numbers:instance1"
Add one to the value on "n1" - this means follow the reverence to get to the actual instance, since you can't increase a reference. This leaves the sheet "Numbers:instance1" holding the value zero + one = one.
Add two to the value on "n2" - this means follow the reverence to get to the actual instance, since you can't increase a reference. This leaves the sheet "Numbers:instance1" holding the value one + two = three.
Does that make sense?
"Can u explain me one question with eg.
passing a value type variable into procedure as an argument.The procedure changes the variable,however when the procedure returns,the variable has not changed.What happened
I think its answer is that passing a value type into procedure creates a copy of the data.
Can u explain this with an e.g if this answer is correct"
You are close, but not quite there.
When you call a method with either a value or a reference, the method gets a copy of the variable. (I'm ignoring ref parameters here). The difference is in the type of the variable which is copied.
When you copy a value type, you copy the whole value - all of the elements of the structure.
When you copy a reference type you only copy the reference.
It doesn't need to be a method call to show this - anything which copies the variable will do:
struct valtype
{
public int i;
public string s;
public valtype(int i, string s) { this.s = s; this.i = i; }
public override string ToString()
{
return string.Format("{0} - {1}", i, s);
}
}
class reftype
{
public int i;
public string s;
public reftype(int i, string s) { this.s = s; this.i = i; }
public override string ToString()
{
return string.Format("{0} - {1}", i, s);
}
}
private void frmMain_Load(object sender, EventArgs e)
{
valtype vt = new valtype(27, "val");
valtype vt2 = vt;
vt.i = 999;
vt.s = "Changed!";
Console.WriteLine(vt);
Console.WriteLine(vt2);
reftype rt = new reftype(17, "ref");
reftype rt2 = rt;
rt.i = 666;
rt.s = "Changed as well!";
Console.WriteLine(rt);
Console.WriteLine(rt2);
}
If you run this code, (and I suggest you do) you will get:
999 - Changed!
27 - val
666 - Changed as well!
666 - Changed as well!
I'm not suggesting this is good code - it has some very bad practices in there - but it shows what I mean.