C# - Passing Parameters by Output



C# out Parameter

A return statement can be used for returning only one value from a function. However, using out parameters, you can return two values from a function. Out parameters are similar to reference parameters, except that they transfer data out of the method rather than into it.

Syntax

Below is the syntax to use out parameters:

void MethodName(out DataType parameter1, out DataType parameter2)

Here is the syntax for calling a method with an out parameter:

MethodName(out variable1, out variable2);

Returning Single Value Using Output Parameters

The output parameters allow methods to return values through parameters without requiring an initial assignment.

Example

The following example demonstrates how a single value is returned from a method using the out parameter:

using System;

namespace CalculatorApplication {
   class NumberManipulator {
      public void getValue(out int x) {
         int temp = 5;
         x = temp; // Assigning a value inside the method
      }
      static void Main(string[] args) {
         NumberManipulator n = new NumberManipulator();
         
         /* Local variable definition */
         int a = 100;
         
         Console.WriteLine("Before method call, value of a: {0}", a);
         
         /* Calling a function to get the value */
         n.getValue(out a);

         Console.WriteLine("After method call, value of a: {0}", a);
         Console.ReadLine();
      }
   }
}

When the above code is compiled and executed, it produces the following result −

Before method call, value of a: 100
After method call, value of a: 5

Returning Multiple Values Using Output Parameters

The output parameters are particularly useful when you need to return multiple values from a method. The variable supplied for the output parameter does not need an initial value.

Example

The following example demonstrates how multiple values can be returned from a method using the out parameters:

using System;

namespace CalculatorApplication {
   class NumberManipulator {
      public void getValues(out int x, out int y) {
          Console.WriteLine("Enter the first value: ");
          x = Convert.ToInt32(Console.ReadLine());
          
          Console.WriteLine("Enter the second value: ");
          y = Convert.ToInt32(Console.ReadLine());
      }
      static void Main(string[] args) {
         NumberManipulator n = new NumberManipulator();
         
         /* Local variable definition */
         int a, b;
         
         /* Calling a function to get the values */
         n.getValues(out a, out b);
         
         Console.WriteLine("After method call, value of a: {0}", a);
         Console.WriteLine("After method call, value of b: {0}", b);
         Console.ReadLine();
      }
   }
}

When the above code is compiled and executed, it produces the following result −

Enter the first value:
7
Enter the second value:
8
After method call, value of a: 7
After method call, value of b: 8
Advertisements