Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not getting the concept of value type and reference type
I have following code but i am not able to understand it how it works
C#
 struct Numbers
{
public int val;
public Numbers(int _val)
{ val = _val; }
public override string ToString()
{ return val.ToString(); }
}
Numbers n1 = new Numbers(0);
Numbers n2 = n1;
n1.val += 1;
n2.val += 2;
Console.WriteLine("n1 = {0}, n2 = {1}", n1, n2);

This code would display “n1 = 1, n2 = 2”

and

C#
class Numbers
{
public int val;
public Numbers(int _val)
{ val = _val; }
public override string ToString()
{ return val.ToString(); }
}
Numbers n1 = new Numbers(0);
Numbers n2 = n1;
n1.val += 1;
n2.val += 2;
Console.WriteLine("n1 = {0}, n2 = {1}", n1, n2);

This code would display “n1 = 3, n2 = 3”

why and how..
please help
Thanx in advance
Posted

The Structs are Value type Object whereas class is an reference type object.

When you are declaring an variable of Struct it is defined as Value Type that means simply the memory variable. In case of struct:

// Variable 1
Numbers n1 = new Numbers(0);

// Variable 2
Numbers n2 = n1;

i.e. a new variable n2 created with value initialized equal to n1. so changing value of n2 would not affect n2

On other end when you create a new instance of Class it is declared as Reference Type. The Object variable of n1 contains a memory address for values.

When you assign n1 to n2, it simply copy the memory address of n1 to n2. Instead of copying direct value. That means value is on some-other memory address and change in value of one instance will reflect in other instance as well..
 
Share this answer
 
Charles Petzold in his free book .NET Book Zero[^] gives an excellent explanation.
 
Share this answer
 
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:
C#
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.
 
Share this answer
 
v2
Comments
shivani 2013 1-Nov-11 5:17am    
As u talked about taking sheets...what i got is i want to tell
in case of reference
1st sheet is just the label n1 and its actual value is stored in 2nd sheet (memory address)and when it is said n2=n1;it means n2 will access the value of n1 which is stored in 2nd sheet and the operations
n2.val+=1
n2.val+=2
will get access from 2nd sheet only
OriginalGriff 1-Nov-11 5:30am    
Sort of - try it and you will see what I mean.
It is easy with pictures - but a lot harder to grasp with just words.
The first sheet is the variable n1 - its content is a reference to the second sheet. It isn't a label, because the value of n1 can be changed (say by creating another, fourth sheet and assigning that reference to n1).
This would not change the value of n2, or of the "Numbers:instance1" sheet value.
shivani 2013 1-Nov-11 5:29am    
am i right
shivani 2013 3-Nov-11 8:33am    
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
OriginalGriff 3-Nov-11 11:41am    
Answer updated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900