Swapping Values Using C#

Background

In this article we will learn  how to swap two values without creating a new storage location using  C#, this type of question might be asked by an interviewer in a .Net position related interview.
My intent for this article is to explain how to answer a question that is often asked in an interview, which is: Write a program to swap two values using C# without creating the new storage location. A candidate new to the interview can become confused because the first problem is the candidate does not even know which keyword to use for that, so how will he or she know how to write the program?
 
So considering the preceding requirement I have decided to write this article with the basics and especially focusing on the beginner, student and whoever might encounter this type of question in an interview.
 
So let us start with the basics.
 
What does swapping values mean?

When the values of two variables are exchanged at runtime it is called swapping of the two values.
Example:
a=100;
b=500;
After swapping:
a=500;
b=100;
Conditions to swap values

The following re the conditions to swap values:
  • The values of two variable are swapped with each other without creating a new storage location for the variables
  • After the swapping of the values of the two variables then before swapped values are need to be remain in that variable.
Depending on the preceding condition we need to swap the values, then after that reading the preceding condition, you are thinking, how is it possible, but it is possible by using the ref keyword of C# that does not create the new storage location for the values.
 
Refer to my following article to learn the details about the ref parameter:
 Next I will explain how to do it step-by-step, as in the following:
  1. Open Visual Studio from "Start" - - "All programs" -- "Microsoft Visual Studio".
  2. Then go to to "File" -> "New" -> "Project..." then select "Visual C#" -> "Windows" -> "Console application".
  3. Then specify the name, "SwappingNum", or whatever name you wish and the location of the project and click on the "OK" button. The new project is created.
Now create the following ref parameter method:
  1. static void SwapNum(ref int x, ref int y)  
  2.       {  
  3.   
  4.           int tempswap = x;  
  5.           x = y;  
  6.           y = tempswap;             
  7.       } 
In the preceding method we are using the ref parameter to take the reference of values from other variables that do not create any new storage location.
Now create the calling method as:
  1. static void Main(string[] args)  
  2.        {  
  3.            int a = 100;  
  4.            int b = 500;  
  5.             
  6.   
  7.            Console.WriteLine( "Value of a and b before sawapping");  
  8.            Console.WriteLine();  
  9.            Console.WriteLine("a=" + " " + a);  
  10.            Console.WriteLine( "b=" + " " + b);  
  11.            SwapNum(ref a, ref b);  
  12.            Console.WriteLine();  
  13.            Console.WriteLine("Value of a and b after sawapping");  
  14.            Console.WriteLine();  
  15.            Console.WriteLine("a=" + " " + a);  
  16.            Console.WriteLine("b=" + " " + b);  
  17.            Console.ReadLine();  
  18.   
  19.        } 
 In the preceding main calling method we are calling the SwapNum method and ing the reference of  a and b variable values to the method, now the entire code will like as follows:
  1. using System;  
  2.   
  3. namespace SwappingValues  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void SwapNum(ref int x, ref int y)  
  8.         {  
  9.   
  10.             int tempswap = x;  
  11.             x = y;  
  12.             y = tempswap;  
  13.                   
  14.         }  
  15.         static void Main(string[] args)  
  16.         {  
  17.             int a = 100;  
  18.             int b = 500;  
  19.               
  20.             Console.WriteLine( "Value of a and b before sawapping");  
  21.             Console.WriteLine();  
  22.             Console.WriteLine("a=" + " " + a);  
  23.             Console.WriteLine( "b=" + " " + b);  
  24.             SwapNum(ref a, ref b);  
  25.             Console.WriteLine();  
  26.             Console.WriteLine("Value of a and b after sawapping");  
  27.             Console.WriteLine();  
  28.             Console.WriteLine("a=" + " " + a);  
  29.             Console.WriteLine("b=" + " " + b);  
  30.             Console.ReadLine();  
  31.   
  32.         }  
  33.     }  

Now run the program; the output will be as:
 
 
 
From the preceding program its clear that we have swapped the values of two variables without creating a new storage location and also the orignal values of  a and b remain as they were, in other words not changed.
 
Note:
  • For detailed code please download the Zip file attached above.
Summary
I hope it is useful for all beginners and other readers. If you have any suggestion regarding this article then please contact me.
 

Up Next
    Ebook Download
    View all
    Learn
    View all