C# Methods, Part 2 - Handling Value-Type Parameters
(Page 2 of 5 )
As you know, a variable of a Value-Type contains the value itself, and when you assign that variable to another variable, it will copy its value into the new variable. The same thing happens when you pass a variable as an argument to a method's parameter; the method makes a copy of the variable's value on its local stack. So the argument and the parameter both refer to separate copies, and any changes made by the method to its own copy (the method parameter) will not be reflected to the calling code variable (the argument). This is the default, as we said before. Consider the following example:
using System;
namespace MyCompany
{
public class MethodTest
{
static void Main()
{
int ValueType = 10;
Console.WriteLine("The value of the variable ValueType = {0}",ValueType);
TakeNumber(ValueType);
Console.WriteLine("After calling the method the value of" + "the variable ValueType = {0}", ValueType);
Console.ReadLine();
}
static void TakeNumber(int x)
{
x++;
Console.WriteLine("Inside the method the parameter x = {0}", x);
}
}
}
The result will be:

The code declares a variable of type integer (ValueType) and assigns the value 10 to it, then it prints this variable's value to the Console window. It then calls the method TakeNumber, and passes the variable to it. Inside the method, we increase the parameter x by one, then print its value (which is 11). The method ends, and we are back to our Main method, where we again print the value of the variable ValueType after the method call, and it's still 10. This proves that changes made to the parameter inside the method don't change or modify the calling code variable (ValueType). The method creates its own copy of the variable (which we call the parameter) and allocates it on its local Stack.
Let's pass the ValueType variable by reference to see what will happen. To pass a Value-Type (or Reference-Type) by reference, you need to use the parameter modifier keyword ref (or out as we will see in the section covering output parameters). You must use the ref keyword in the method declaration, with method invocation. When you use the ref keyword, you actually pass a reference to the data, and there is no data copying process. Changes made to the method's parameter will be reflected to the calling code variable. Modify the above code as follows:
using System;
namespace MyCompany
{
public class MethodTest
{
static void Main()
{
int ValueType = 10;
Console.WriteLine("The value of the variable ValueType = {0}",ValueType);
TakeNumber(ref ValueType);
Console.WriteLine("After calling the method the value of" + "the variable ValueType = {0}", ValueType);
Console.ReadLine();
}
static void TakeNumber(ref int x)
{
x++;
Console.WriteLine("Inside the method the parameter x = {0}", x);
}
}
}
Here's the result:

This time the result is different. Inside the method, the parameter x = 11, and after the method terminates the calling code variable has been assigned 11. To make it easy for you, think of passing a Value-Type by reference as follows: first, you declare the method's parameter with the ref keyword, then you call the method and pass the Value-Type, also with the ref keyword. The ref keyword actually does one thing with Value-Type: it tells the CLR where the value type is. When the method modifies the parameter and terminates, the new value is copied back to the location where the Value-Type lives.
I can't tell you when to pass a Value-Type by reference because it may fit in many scenarios. For example, you have a class that contains a method that accepts three Value-Type parameters and, as you know, that methods return only one value back. But in your scenario, you need to change these three variables, so the perfect solution is to make your method as void, and declare the parameters with the ref keyword, so any changes made inside the method will affect the variables.
Let's take a look at how passing by value and by reference affects Reference-Types.
Next: Handling Reference-Type Parameters >>
More C# Articles
More By Michael Youssef