Difference between passing reference types by ref and by value

We all know the difference between passing value types byval and byref, if the variable is passed byval any change to the variable value in the called function is not reflected back in the callee because a copy of the variable is passed, whereas passing it byref means that the changes will be reflected back because the same variable is passed.

What happens when a reference type is passed by byval or byref?

Lets take a look at the example code:

static void Main()
{
Hashtable hst = new Hashtable(3);
hst.Add(1,1);
PassHashTableByVal(hst); //by default .net parameters are passed by val
Console.WriteLine("Count after passing byval: {0}",hst.Count); //will print 2
PassHashTableByRef(ref hst);
Console.WriteLine("Count after passing byref: {0}", hst.Count); //will print 3
Console.Read();
}
static void PassHashTableByVal(Hashtable h1)
{
h1.Add(2,2);
}
static void PassHashTableByRef(ref Hashtable h2)
{
h2.Add(3,3);
}

If you run the above code you will notice that irrespective of byval or byref, the original hashtable is affected in both the cases. So does it mean that there is no difference between passing a reference type object byval or byref? Hold on there is a subtle difference.....

A Brief Insight into Reference Types:

Data for Reference types is stored on the heap and a pointer (which points to the data on the heap) is created on the stack, whenever an instance of reference type is created the pointer is returned back and is used to manipulate the data on the stack. Hence Hashtable hst = new HashTable(); actually returns back the pointer. Now when this pointer is passed by val, all we are doing is duplicating the pointer but it still points to the same memory on the heap and hence any manipulation done to the object in the called method will manipulate the same data to which original hashtable pointer was pointing. In case of passing by ref, the original pointer itself is passed to the called function.

So what is the difference between passing byref v/s passing byval? Lets take a look at the slightly modified version of above code:

static void Main()
{
Hashtable hst = new Hashtable(3);
hst.Add(1,1);
PassHashTableByVal(hst); //by default .net parameters are passed by val
Console.WriteLine("Count after passing byval: {0}",hst.Count); //will print 2
PassHashTableByRef(ref hst);
Console.WriteLine("Count after passing byref: {0}", hst.Count); //will throw a null reference exception.
Console.Read();
}
static void PassHashTableByVal(Hashtable h1)
{
h1.Add(2,2);
h1 = null;
}
static void PassHashTableByRef(ref Hashtable h2)
{
h2.Add(3,3);
h2 = null;
}

A null reference exception in case of passing hashtable as by ref is thrown because in the PassHashTableByRef() we are setting h2 to null which actually is same as hst, whereas in case of passing it by val we are only setting the copy of the hst pointer to null.

Hope this article explained the subtle difference between passing reference type by val or by ref.


Similar Articles