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?
- A Method contains a number of statements to perform a particular task.
- A method can have any number of statements inside it.
- In C#, every program must have a method named "Main". "Main" method is the starting point of a program.
- First, we define a method and then we make a call to use it.
- The syntax of a method declaration is,
- <access_specifier>
- <return_type>
- <method_name>() {
- The syntax of a method call is,
Passing Parameters to a method
Following are the ways to pass parameters to a method,
- Value parameters
- Reference parameters
- 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
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
- static void Main(string[] args)
- {
- int result;
- int a = 25;
- int b = 75;
- result = a + b;
- Console.WriteLine("Result: " + result);
-
- Program p = new Program();
-
- p.Addition(2, 5);
- Console.WriteLine("Result: " + result);
- Console.ReadKey();
-
- }
- public void Addition(int a, int b)
- {
- int result = a + b;
- Console.WriteLine("Result inside method: " + result);
- }
- }
-
- }
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
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
- static void Main(string[] args)
- {
- int a = 25;
- int b = 75;
- Console.WriteLine("Value of a= " + a + " and b= " + b);
-
- Program p = new Program();
-
- p.StringSwapping(ref a, ref b);
- Console.WriteLine("Value of a= " + a + " and b= " + b);
- Console.ReadKey();
-
- }
- public void StringSwapping(ref int c, ref int d)
- {
- int temp;
- temp = d;
- d = c;
- c = temp;
- Console.WriteLine("Inside method: Value of a= " + c + " and b= " + d);
- }
- }
-
- }
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
- using System;
-
- namespace Tutpoint
- {
- class Program
- {
- static void Main(string[] args)
- {
- int a = 25;
- int b = 75;
- Console.WriteLine("Value of a= " + a + " and b= " + b);
-
- Program p = new Program();
-
- p.ValueOutput(out a, out b);
- Console.WriteLine("Value of a= " + a + " and b= " + b);
- Console.ReadKey();
-
- }
- public void ValueOutput(out int c, out int d)
- {
- d = 100;
- c = 1000;
- Console.WriteLine("Inside method: Value of c= " + c + " and d= " + d);
- }
- }
-
- }
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.