swapping Using Ref //swap - CS: Using Namespace Class Public Int Public Int Public Int Int

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

"If C# manipulates objects by reference, will a swap work?

" - This issue


must certainly have troubled quiet a lot of beginners. C# does manipulate
objects by reference, and all object variables are references. On the other
hand, C# does not pass method arguments by reference; it passes them by
value, (even if the method arguments are of reference type). Thus, a regular
swap method will not work!

Let's come from the beginning, we know that there are four kinds of formal
parameters in a C# method declaration, namely

 Value parameters, which are declared without any modifiers. (default)


 Reference parameters, which are declared with the ref modifier.
 Output parameters, which are declared with the out modifier.
 Parameter arrays, which are declared with the params modifier.

Thus if there is no any method parameter keyword (ref or out), the


parameter can have a value associated with it. That value can be changed in
the method, but that value will not be reflected when the control passes
back to the calling procedure. This is true for all value types. Now, consider
a reference type, an object. If we pass an object to a method and if the
value of its member is changed, it will be retained even when the control is
passed back to the calling procedure! This is because objects are
manipulated by reference.

If so, then, if we pass two objects to a regular swap method and come back
to the calling procedure to see whether they have got swapped, they would
have not! Look down the code below,

//swapping using ref


//swap.cs
using System;
namespace swap
{
class class1
{
public int x;
public int y;
public class1(int xval, int yval)
{
x=xval;
y=yval;
}
}
class SwapTest
{
private static void swap_ref(ref class1 param1, ref class1 param2)
{
class1 temp = param1;
param1 = param2;
param2 = temp;
}
private static void badswap_noref(class1 param1, class1 param2)
{
param1.x = 100;//even though the references are passed by value,
param1.y = 200;//we could very well assign or alter the values of the parameters
class1 temp = param1;//but swap won't work
param1 = param2;
param2 = temp;//only the method references are swapped not the original ones
}
public static void Main(string [] args)
{
//instantiating
class1 obj1 = new class1(0,0);
class1 obj2 = new class1(0,0);
//initial values of the instances obj1 and obj2
Console.WriteLine(" ");
Console.WriteLine("X: " +obj1.x + " Y: " +obj1.y);
Console.WriteLine("X: " +obj2.x + " Y: " +obj2.y);
Console.WriteLine(" ");
//calling badswap
badswap_noref(obj1,obj2);//the two object references are passed by value to
badswap_noref' method
Console.WriteLine("after badswap_noref");
Console.WriteLine("X: " +obj1.x + " Y:" +obj1.y);
Console.WriteLine("X: " +obj2.x + " Y:" +obj2.y);
Console.WriteLine(" "); 
//calling swap_ref
Console.WriteLine("after swap_ref");
swap_ref(ref obj1,ref obj2);//passing the objects by reference explicitly
Console.WriteLine("X: " +obj1.x + " Y:" +obj1.y);
Console.WriteLine("X: " +obj2.x + " Y:" +obj2.y);
Console.WriteLine(" ");
}
}
}

Output of the above program is as follows


Fig 1. Output of swap.cs

In the above program, we pass the instances obj1 and obj2 of class1 to
badswap_noref() method and see that they are not actually getting
swapped. This is what actually happens in the badswap_noref method. Not
the actual references obj1 and obj2 are passed. But a copy of the references
is passed. So param1 and param2 contains a copy of the actual references
obj1 and obj2 respectively. We are able to modify the values of X and Y in
the badswap_noref method thru param1 and param2, because param1 and
param2 still points to the same address locations referred to by obj1 and
obj2. But what we are swapping is only param1 and param2, the method
references, which are not retained when we come back to the calling
procedure, Main().

Fig 2. Object reference and method reference


Fig 3. Before and after badswap_noref

Thus, in C#, a swap method can be made possible by using the ref keyword.
In such a case, we pass the object references explicitly by reference and not
by value, thus able to swap them right in the called method.

Fig 4. Before and after swap_ref

You might also like