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>() { // statements }
- The syntax of a method call is,
- <method_name>();
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: 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.

Lukasz KlusekPosted Jan 16, 2018, 5:39 AM
Worth to mention what is difference between ref and out. ref tells the compiler that the object is initialized before entering the function, while out tells the compiler that the object will be initialized inside the function.