Passing Parameters To A Method In C#

In this blog, I am going to share with you about Passing Parameters to a method in C#.
 
Before I discuss passing parameters to a method,  I will first give a brief intro of the method. I will not go into the details.
 
What is Method?
  1. A Method contains a number of statements to perform a particular task.
  2. A method can have any number of statements inside it.
  3. In C#, every program must have a method named "Main". "Main" method is the starting point of a program.
  4. First, we define a method and then we make a call to use it.
  5. The syntax of a method declaration is,
    1. <access_specifier>  
    2.     <return_type>  
    3.         <method_name>() { // statements }  
  6. The syntax of a method call is,
    1. <method_name>();   
Passing Parameters to a method
 
Following are the ways to pass parameters to a method,
  1. Value parameters
  2. Reference parameters
  3. Output parameters
Value parameters
  • A new storage location is created for every value parameter.
  • This is the default approach to pass parameters to a method.
  • When a value- type variable is passed to a method it means that we are passing a copy of the variable to the method. So the method does not have any affects outside the method. This is just the opposite of the reference type variable.
I have written a simple program to show how value-type passes are passed inside a method and how it actually works. First I created three value type variables (a,b, result) inside "Main" method, performed adding of a,b and then printed the result on the console. Second, I created a method with two integer arguments as variables declared with int are value-type variables. So separate location for variables of method Addition() will be created due to which it does not have any effects nor does it change value outside the method.
 
Code
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             int result;  
  10.             int a = 25;  
  11.             int b = 75;  
  12.             result = a + b;  
  13.             Console.WriteLine("Result: " + result);  
  14.   
  15.             Program p = new Program();  
  16.   
  17.             p.Addition(2, 5);  
  18.             Console.WriteLine("Result: " + result);  
  19.             Console.ReadKey();  
  20.   
  21.         }  
  22.         public void Addition(int a, int b)  
  23.         {  
  24.             int result = a + b;  
  25.             Console.WriteLine("Result inside method: " + result);  
  26.         }  
  27.     }  
  28.   
  29. }  
Output

Result: 100
Result inside method: 7
Result: 100

Reference parameters
  • Unlike Value-type, no new storage location is created for reference parameters. In spite of it, a reference to the same memory location of a variable is said to be a reference parameter.
  • ref keyword is used to declare reference type variable on method. It is also required while making a call to a method.
  • When a Reference- type variable is passed to a method it means that we are passing the reference to the same memory location of a variable as that of actual parameters. So when a change is made in a method, the change also gets reflects outside of it.
I have written a simple program to show how reference-type parameters are passed inside a method and how it actually works. First I define two variables (a and b) inside "Main" method and print their values on the console. Second, I created a method and pass two reference-type variables using ref keyword and perform swapping between the two numbers. ref keyword on method called StringSwapping(ref a, ref b) will pass the reference to the memory location of variable a and b. So, any changes made to the value of these variables inside the method will also reflect outside the method.
 
Code
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             int a = 25;  
  10.             int b = 75;  
  11.             Console.WriteLine("Value of a= " + a + " and b= " + b);  
  12.   
  13.             Program p = new Program();  
  14.   
  15.             p.StringSwapping(ref a, ref b);  
  16.             Console.WriteLine("Value of a= " + a + " and b= " + b);  
  17.             Console.ReadKey();  
  18.   
  19.         }  
  20.         public void StringSwapping(ref int c, ref int d)  
  21.         {  
  22.             int temp;  
  23.             temp = d;  
  24.             d = c;  
  25.             c = temp;  
  26.             Console.WriteLine("Inside method: Value of a= " + c + " and b= " + d);  
  27.         }  
  28.     }  
  29.   
  30. }  
Output

Value of a= 25 and b= 75
Inside method: Value of a= 75 and b= 25
Value of a= 75 and b= 25
 
Output parameters
  • Output parameters are used to return more than one value from a method. From return keyword, we are only able to return one value.
  • out keyword is used to declare Output type parameters on a method. It is also required while making a call to a method.
  • This is similar to reference-type except that multiple values are returned from the method.
I have written a simple program to show how Output-type parameters are passed inside a method and how it actually works. First I define two variables (a and b) inside "Main" method and print their values on the console. Second, I created a method ValueOutput() and pass two Output-type parameters using out keyword. So, ValueOutput() method will return the value of both variables a and b.
 
Code
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             int a = 25;  
  10.             int b = 75;  
  11.             Console.WriteLine("Value of a= " + a + " and b= " + b);  
  12.   
  13.             Program p = new Program();  
  14.   
  15.             p.ValueOutput(out a, out b);  
  16.             Console.WriteLine("Value of a= " + a + " and b= " + b);  
  17.             Console.ReadKey();  
  18.   
  19.         }  
  20.         public void ValueOutput(out int c, out int d)  
  21.         {  
  22.             d = 100;  
  23.             c = 1000;  
  24.             Console.WriteLine("Inside method: Value of c= " + c + " and d= " + d);  
  25.         }  
  26.     }  
  27.   
  28. }  
Output

Value of a= 25 and b= 75
Inside method: Value of c= 1000 and d= 100
Value of a= 1000 and b= 100
 
Thank you. Please feel free to ask any questions or make a suggestion.
Ebook Download
View all
Learn
View all