Open In App

Out Parameter With Examples in C#

The out is a keyword in C# which is used for the passing the arguments to methods as a reference type. It is generally used when a method returns multiple values.

Important Points:



Declaration of out Parameter:  

// No need to initialize 
// the variable here
data_type variable_name;

Method_Name(out variable_name);

// you can also convert both above two 
// lines of codes as follows from
//  C# 7.0 onwards
Method_Name(out data_type variable_name);

Here the value of variable_name must be initialized in the called method before it returns a value. 



Example: 




// C# program to illustrate the
// concept of out parameter
using System;
 
class GFG {
 
    // Main method
    static public void Main()
    {
 
        // Declaring variable
        // without assigning value
        int i;
 
        // Pass variable i to the method
        // using out keyword
        Addition(out i);
 
        // Display the value i
        Console.WriteLine("The addition of the value is: {0}", i);
    }
 
    // Method in which out parameter is passed
    // and this method returns the value of
    // the passed parameter
    public static void Addition(out int i)
    {
        i = 30;
        i += i;
    }
}

Output: 
The addition of the value is: 60

 

Multiple out Parameters: In C#, a user is allowed to pass multiple out parameters to the method and the method returns multiple values. 

Example: In the below code, we declared two value variables without initializing i.e int i, j;. Now we pass these parameters to the Addition method using out keyword like Addition(out i, out j);. The value of these variables is assigned in the method in which they passed. 




// C# program to illustrate the
// concept of multiple out parameter
using System;
 
class GFG {
 
    // Main method
    static public void Main()
    {
 
        // Declaring variables
        // without assigning values
        int i, j;
 
        // Pass multiple variable to
        // the method using out keyword
        Addition(out i, out j);
 
        // Display the value i and j
        Console.WriteLine("The addition of the value is: {0}", i);
        Console.WriteLine("The addition of the value is: {0}", j);
    }
 
    // Method in which out parameters
    // are passed and this method returns
    // the values of the passed parameters
    public static void Addition(out int p, out int q)
    {
        p = 30;
        q = 40;
        p += p;
        q += q;
    }
}

Output: 
The addition of the value is: 60
The addition of the value is: 80

 

Enhancement of Out Parameter in C# 7.0 : In C# 7.0, there are some new features added to the out parameter and the features are: 

Example: Below programs demonstrate the inline declaration of Out parameter. Here the line of code i.e Area(out int length, out int width, out int Rarea); contains the inline declaration of Out parameter as these variables are directly declared inside the method calling. The value of the variables is initialized in the method in which they passed.

Note: You need to require C# 7.0 version to run this example.

Example:  




// C# program to illustrate the
// concept of out parameter
using System;
 
class GFG
{
 
    // Main method
    static public void Main()
    {
 
        // In-line declaring variables
        // without assigning values
        // Passing multiple variable to
        // the method using out keyword
        Area(out int length, out int width, out int Rarea);
 
        // Display the value length, width, and Rarea
        System.Console.WriteLine("Length of the rectangle is: "+ length);
        System.Console.WriteLine("Width of the rectangle is: "+ width);
        System.Console.WriteLine("Area of the rectangle is: "+ Rarea);
        Console.ReadLine();
    }
 
    // Method in which out parameters are passed
    // and this method returns the values of
    // the passed parameters
    public static void Area(out int p, out int q, out int Rarea)
    {
        p = 30;
        q = 40;
        Rarea = p * q;
    }
}

Output: 

Length of the rectangle is  : 30
Width of the rectangle is : 40
Area of the rectangle is : 1200

 


Article Tags :
C#