Pass parameters by value type and reference type

pass by value and reference

Sometimes it could be frustrating to debug the code if the values of a variable are in changing in an unexpected manner. To handle this kind of situation, it is important to know when to pass parameters by value and reference.

Pass by value:

When you pass parameters as value, .net will create a copy of it and pass it. Since the compiler passes copy, any modification made to that “copy” will not affect the original one.

By default, arguments are passed by value. Example:

static void Main(string[] args)
{
	int i = 10;
	
	Modify(i);
	Console.WriteLine(i); // this will print 10
	Console.ReadLine();
}

private static void Modify(int i)
{
	i = 20;
}

This program will print “10” since we passed a copy of “i” and modifications were done on the copy.

Pass by reference:

When you pass arguments as a reference, the compiler will actually pass its address. That means, whatever modifications happen on that (reference/address), it will affect the caller.

Example:

static void Main(string[] args)
{
    int i = 10;

    Modify(ref i);
    Console.WriteLine(i); // this will print 20
    Console.ReadLine();
}

private static void Modify(ref int i)
{
    i = 20;
}

This program will print “20” because we passed “i” as a reference and caller modified the reference.

Interesting Scenarios:

Now we will try a program and see the output.

static void Main(string[] args)
{
	int[] i = new int[1];
	i[0] = 10;

	Modify(i);
	Console.WriteLine(i[0]);
	Console.ReadLine();
}

private static void Modify(int[] i)
{
	i[0] = 20;
}

What would be the output of this program? It will be 20. But why? We passed “i” by value/copy, right? That means modifications should not have been reflected in the caller but they did. The reason is, we used array here, array are reference types and you know they don’t store data within themselves but in a heap and have a reference to that memory in the heap. It is right that we passed the copy but that copy also pointed to the same location in the memory and we updated the memory that copy was pointing to. Hence original “i” will read the same memory which was updated with the value “20”.

One more scenario.

static void Main(string[] args)
{
    string i = "original";

    Modify(i);
    Console.WriteLine(i);
	    Console.ReadLine();
}

private static void Modify(string i)
{
	    i = "modified";
}

In this example we used string. The string is a reference type hence should have updated the original “i” but it doesn’t. It is because strings are immutables. That means, they can not be modified. When we modify it, it actually creates a copy of it. 

Find more about value type and reference type here.

Summary:

To summarise when to pass parameters by value type and reference type, consider the below scenarios.

When you pass parameters as a value then any changes to them inside the target methods will not affect the original one. Because behind the scene, the compiler will create a copy of those parameters and pass them and the target method will modify the copies.

When you pass parameters as a reference then any changes to them inside the target method will affect the original one. Because behind the scene, the compiler will pass the address of the parameters.

CategoriesC#

Leave a Reply

Your email address will not be published.

Coding Crest Back To The Top