C# Methods, Part 2 - Handling Reference-Type Parameters
(Page 3 of 5 )
When you pass an object variable to a method parameter, the default behavior is to copy by value -- but in the case of a Reference-Type, the copied value is not the object itself. In other words, the copied value is not the object fields and methods. What actually happens is that a reference to the object is just copied from the object variable to the method parameter, and this is similar for to having a Value-Type passed by reference (using the ref keyword).
When you pass a Value-Type by reference, a reference to the variable is passed to the method. This means that both the variable (which contains the value itself) and the method parameter (which contains a reference to the value) refer to the same data. Passing a Reference-Type by value differs. When you pass a Reference-Type by value, a copy of the object reference is passed to the method parameter, which means that we have one object located on the Heap, with two references located on the Stack. Actually, it is worth noting that the object reference is a value that lives on the Stack, and it refers to the actual object on the Heap.
So let's look at an example:
using System;
namespace MyCompany
{
public class MethodTest
{
static void Main()
{
Order order = new Order();
order.OrderNumber = 12345;
Console.WriteLine("order.OrderNumber = {0}",order.OrderNumber);
TakeObject(order);
Console.WriteLine("after the method call " + "order.OrderNumber = {0}",order.OrderNumber);
Console.ReadLine();
}
static void TakeObject(Order ord)
{
ord.OrderNumber = 54321;
Console.WriteLine("ord.OrderNumber = {0}", ord.OrderNumber);
}
}
public class Order
{
public int OrderNumber;
}
}
The result will be:

As you can see, the Main method creates an object of type Order and assigns the value 12345 to the OrderNumber field of that object. Then it prints to the Console the value of the object's field, calls the static method TakeObject, and passes the order object to it. The method assigns a new value to the parameter and terminates. The Main method again prints the value of the object's field and, as you can see, it has been changed after the method call. To make it clear that the method just has another reference to the object, take a look at the following revised version of the above code:
using System;
namespace MyCompany
{
public class MethodTest
{
static void Main()
{
Order order = new Order();
order.OrderNumber = 12345;
Console.WriteLine("order.OrderNumber = {0}",order.OrderNumber);
TakeObject(order);
Console.WriteLine("after the method call " + "order.OrderNumber = {0}",order.OrderNumber);
Console.ReadLine();
}
static void TakeObject(Order ord)
{
ord = null;
}
}
public class Order
{
public int OrderNumber;
}
}
Now the result is:

We just assign null to the method parameter to prove that it's just another reference. When we assigned the reference to null, the object had not been affected, and the order .OrderNumber still equals to the same value (12345).
You can also pass a Reference-Type by reference, but it's dangerous and should be avoided except for the situations that you are sure you need it. For example, you can pass an object to a method (by reference using the ref keyword) and the method can destroy your object, or even assign it to a new, different one. The next example is a modified version of the above code:
using System;
namespace MyCompany
{
public class MethodTest
{
static void Main()
{
Order order = new Order();
order.OrderNumber = 12345;
Console.WriteLine("order.OrderNumber = {0}",order.OrderNumber);
TakeObject(ref order);
Console.WriteLine("after the method call " + "order.OrderNumber = {0}",order.OrderNumber);
Console.ReadLine();
}
static void TakeObject(ref Order ord)
{
Order newOrder = new Order();
newOrder.OrderNumber = 987123;
ord = newOrder;
Console.WriteLine("The method assign a new object to the parameter" + "and passing 987123 to OrderNumber");
}
}
public class Order
{
public int OrderNumber;
}
}
The result is:

The method TakeObject takes an Order object parameter and destroys it by creating a new one and assigning a new value to the field OrderNumber. So passing a Reference-Type by reference makes a double reference. In other words, it makes a reference to the reference, which refers to the object. In our example, when we pass the order object reference to the method, the ref keyword creates a reference to the order object reference, and when the method makes any changes it will be reflected to the reference (because we have a reference to the object reference). In our example, we assigned a new object to the parameter ord, which in turn assigned this new object reference to the calling code object reference.
Next: The out keyword >>
More C# Articles
More By Michael Youssef