Methods Parameters In C#

There are mainly four types of method parameters.
  1. Value Type
  2. Reference Type
  3. Out parameters
  4. Parameter Arrays
Before discussing them, let's find out the difference between the two terms- Method Parameters and Method Arguments: 
 
For e.g., we have a method given below.
  1. public static void test(string p) 
  2.             { 
  3.  
  4.  }  
In the above method method,  p is called method parameter. On the other hand, if we try to invoke this method, the command will be, as given below.
  1. test("abc")  
In this case "abc" is the method argument. 
 
Let's discuss different types of method parameters.
 
Value Type

In value type method parameters, argument and parameter holds different locations in the memory. Thus, if the value of the parameter gets changed, it will not affect the value of the argument.

The example is given below.
  1.  class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.    //Lets invoke MethodForValueType Method  
  6.             int a = 100;  
  7.             MethodForValueType(a);  
  8.            Console.WriteLine(a);  
  9. }  
  10. }  
  11.   
  12.   
  13.   
  14. //Let we have a method expecting int parameter.  
  15.         public static void MethodForValueType( int b)  
  16.         {  
  17.             b= 20;  
  18.         }  
  19. }  
In the example shown above,  we passed a=100 to "MethodForValueType", the copy of the same value will be created in the memory and b will hold that value. In this case, a and b will occupy two different places in the memory.

If we change the value of b, it will not affect the value of a because both variables are referring to different memory locations.
 
 
 
The value of a will remain 100, as shown above.
 
Reference Type

In Reference type method parameters, argument and parameter have the same location in the memory. If the value of the parameter gets changed, it will not effect the value of the argument.

The example is given below.
  1.    
  2.   
  3.   
  4.   
  5.  class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9. //Lets invoke MethodForRefTypeMethod  
  10. int a = 100;  
  11.   MethodForRefType(ref a);  
  12. Console.WriteLine(a);
  13. }  
  14. //Reference Type:  
  15.         //Let we have a method expecting reference int parameter.  
  16.         public static void MethodForRefType(ref int b)  
  17.         {  
  18.             b = 20;  
  19.         }  
  20. }  
Now, as we pass a=100 to MethodForRefType, a and b will point to the same reference in the memory i.e. location of the variable a. If we change the value of b, it will effect the value of both the variables because they are referring to the same memory location.
 
Lets see the output for a in the screenshot given below.

 
 
Output parameters

If we want to return one value from a function, we use return type methods.But if we need to return more than one different values from a single method then we make use of output parameters.

Let's have an example.
  1.  class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.              //Lets invoke MethodForValueType Method  
  6.             int a = 100, b = 80;  
  7.             int Total = 0,subtraction = 0;  
  8.             //we can directly get the output from method in out parameters.  
  9.             SumSub(a, b, out Total, out subtraction);  
  10.             Console.WriteLine("Sum is: {0}", Total);  
  11.             Console.WriteLine("Sub is:{0}", subtraction);  
  12.   
  13.  public static void SumSub(int a, int b, out int Sum, out int Sub)  
  14.         {  
  15.             Sum = a + b;  
  16.             Sub = a - b;  
  17.         }  
  18. }  


Parameter Array
  1. As the name suggests, the parameter array is used, where we need to pass an array as a parameter.
  2. For the parameter array, we need to use keyword array.
  3. If we want to make an array as optional we can use the keyword params, else it will work same as value type, as shown below.
  1. class Program  
  2.    {  
  3. static void Main(string[] args)  
  4.        {  
  5.            int[] numbers=new int[3];  
  6.            numbers[0]=1;  
  7.             numbers[1]=2;  
  8.             numbers[2]=3;  
  9.            //because if define params keyword then it makes it optional.  
  10.             PrintFromArrayParameter(numbers);  
  11.             PrintFromArrayParameter(); 
  12.   
  13.   
  14.   
  15. public static void PrintFromArrayParameter(params int[] numbers)  
  16.        {  
  17.            foreach (var item in numbers)  
  18.            {  
  19.                Console.WriteLine(item);  
  20.            }  
  21.        }  
  22. }
Let's see the output that we get, as shown below.

Next Recommended Reading Passing Parameters To A Method In C#