What are different methods of passing parameters in C#?

When a method with parameters is called, you need to pass the parameters to the method using one of the following three methods in C# −

Value Parameters (Default)

This method copies the actual value of an argument into the formal parameter of the function. Changes made to the parameter inside the function have no effect on the original argument because the method works with a copy of the value.

Value Parameters - Copy by Value Main Method int a = 7 int b = 10 swap Method int x = 7 int y = 10 COPY Original values remain unchanged Copies are modified then destroyed

Example

using System;

class NumberManipulator {
   public void swap(int x, int y) {
      int temp;
      temp = x; /* save the value of x */
      x = y;    /* put y into x */
      y = temp; /* put temp into y */
      Console.WriteLine("Inside swap method - x: {0}, y: {1}", x, y);
   }

   static void Main(string[] args) {
      NumberManipulator n = new NumberManipulator();
      
      /* local variable definition */
      int a = 7;
      int b = 10;
      
      Console.WriteLine("Before swap, value of a : {0}", a);
      Console.WriteLine("Before swap, value of b : {0}", b);
      
      /* calling a function to swap the values */
      n.swap(a, b);
      
      Console.WriteLine("After swap, value of a : {0}", a);
      Console.WriteLine("After swap, value of b : {0}", b);
   }
}

The output of the above code is −

Before swap, value of a : 7
Before swap, value of b : 10
Inside swap method - x: 10, y: 7
After swap, value of a : 7
After swap, value of b : 10

Reference Parameters (ref keyword)

This method passes the reference to the memory location of an argument into the formal parameter. Changes made to the parameter affect the original argument because both refer to the same memory location.

Syntax

public void MethodName(ref dataType parameterName) {
   // method body
}

// Call the method
MethodName(ref variableName);

Example

using System;

class NumberManipulator {
   public void swap(ref int x, ref int y) {
      int temp;
      temp = x; /* save the value of x */
      x = y;    /* put y into x */
      y = temp; /* put temp into y */
      Console.WriteLine("Inside swap method - x: {0}, y: {1}", x, y);
   }

   static void Main(string[] args) {
      NumberManipulator n = new NumberManipulator();
      
      int a = 7;
      int b = 10;
      
      Console.WriteLine("Before swap, value of a : {0}", a);
      Console.WriteLine("Before swap, value of b : {0}", b);
      
      /* calling function with ref keyword */
      n.swap(ref a, ref b);
      
      Console.WriteLine("After swap, value of a : {0}", a);
      Console.WriteLine("After swap, value of b : {0}", b);
   }
}

The output of the above code is −

Before swap, value of a : 7
Before swap, value of b : 10
Inside swap method - x: 10, y: 7
After swap, value of a : 10
After swap, value of b : 7

Output Parameters (out keyword)

The out keyword is used when you want a method to return multiple values. The parameter does not need to be initialized before passing, but the method must assign a value to all out parameters before returning.

Syntax

public void MethodName(out dataType parameterName) {
   parameterName = value; // must assign before return
}

// Call the method
MethodName(out variableName);

Example

using System;

class Calculator {
   public void Calculate(int x, int y, out int sum, out int product) {
      sum = x + y;
      product = x * y;
   }

   static void Main(string[] args) {
      Calculator calc = new Calculator();
      
      int a = 15, b = 25;
      int addition, multiplication;
      
      Console.WriteLine("Input values: a = {0}, b = {1}", a, b);
      
      /* calling method with out parameters */
      calc.Calculate(a, b, out addition, out multiplication);
      
      Console.WriteLine("Sum: {0}", addition);
      Console.WriteLine("Product: {0}", multiplication);
   }
}

The output of the above code is −

Input values: a = 15, b = 25
Sum: 40
Product: 375

Comparison of Parameter Passing Methods

Method Keyword Initialization Required Changes Affect Original Use Case
Value Parameters None (default) Yes No Pass data for reading only
Reference Parameters ref Yes Yes Modify existing variables
Output Parameters out No Yes Return multiple values

Conclusion

C# provides three methods for passing parameters: value parameters (default) create copies and don't affect originals, ref parameters pass references allowing modifications to original variables, and out parameters enable methods to return multiple values. Choose the appropriate method based on whether you need to modify the original data or return multiple results.

Updated on: 2026-03-17T07:04:35+05:30

474 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements