What are different types of parameters to a method in C#?

CsharpServer Side ProgrammingProgramming

Methods in C# are generally the block of codes or statements in a program which gives the user the ability to reuse the same code which ultimately saves the excessive use of memory, acts as a time saver and more importantly, it provides better readability of the code.

There might be certain situations the user wants to execute a method but sometimes that method requires some valuable inputs in order to execute and complete its tasks. These input values are known as Parameters.

Parameters can be passed to a method in the following ways −

  • Value Parameters

  • Reference Parameters

  • Output Parameters

Value Parameters

Value Parameters copies the actual value of an argument into the formal parameter of the function. When a simple variable is passed as the parameter to any method, it is passed as a value. This means that the value contained by the variable that is passed as the parameter is copied to the variables of the method, and if inside the method these values are changed or modified, the change is not reflected in the actual passed variable. Most of the primitive data types such as integer, double, Boolean etc. are passed by value.

Example

 Live Demo

using System;
namespace MyApplication{
   public class Program{
      public static void Main(){
         int x = 5, y = 5;
         Console.WriteLine($"Value before calling the method. x = {x}, y = {y}");
         ValueParamter(x, y);
         Console.WriteLine($"Value after calling the method. x = {x}, y = {y}");
      }
      public static void ValueParamter(int x, int y){
         x = 10;
         y = 10;
         int z = x + y;
         Console.WriteLine($"Sum of x and y = {z}");
      }
   }
}

Output

The output of the above code is as follows &mius;

Value before calling the method. x = 5, y = 5
Sum of x and y = 20
Value after calling the method. x = 5, y = 5

Reference Parameters

Reference Parameters copies the reference to the memory location of an argument into the formal parameter. Normally, all the objects are passed by reference as parameter to the method. The method operates on the references of the variables passed in the parameters rather than operating on their values. This results in the modification of variables in the calling function when they are modified in the called function. This means that changes made to the parameter affect the argument.

Example

 Live Demo

using System;
namespace MyApplication{
   public class Program{
      public static void Main(){
         int x = 5, y = 5;
         Console.WriteLine($"Value before calling the method. x = {x}, y = {y}");
         RefParamter(ref x, ref y);
         Console.WriteLine($"Value after calling the method. x = {x}, y = {y}");
      }
      public static void RefParamter(ref int x, ref int y){
         x = 10;
         y = 10;
         int z = x + y;
         Console.WriteLine($"Sum of x and y = {z}");
      }
   }
}

Output

The output of the above code is as follows −

Value before calling the method. x = 5, y = 5
Sum of x and y = 20
Value after calling the method. x = 10, y = 10

Output Parameters

Output Parameters helps in returning more than one value. A return statement can be used for returning only one value from a function. However, using output parameters, you can return two values from a function. The variable supplied for the output parameter need not be assigned a value. Output parameters are particularly useful when you need to return values from a method through the parameters without assigning an initial value to the parameter. Output parameters are similar to reference parameters, except that they transfer data out of the method rather than into it.

Example

 Live Demo

using System;
namespace MyApplication{
   public class Program{
      public static void Main(){
         int result;
         OutParamter(out result);
         Console.WriteLine($"Result: {result}");
      }
      public static void OutParamter(out int result){
         int x = 10, y = 10;
         result = x + y;
      }
   }
}

Output

The output of the above code is as follows:
Result: 20
raja
Published on 04-Aug-2020 06:46:18
Advertisements