C# Out Parameter

In C# coding language, the out keyword is primarily employed for passing arguments by reference. It functions similar to a reference type, but it eliminates the need to initialize a variable before passing it. The out keyword is essential for passing arguments as an out type, especially when a function needs to return multiple values.

Syntax

It has the following syntax:

Example

void MethodName (out DataType parameter1, out DataType parameter2)

{

    // Assigning the value

    parameter1 = value1;

    parameter2 = value2;

}

In this syntax,

  • void: It is the void type method, which means the method does not return a value directly.
  • DataType: It defines the type of data that has been passed to the method.
  • Parameter: It is used for holding the value that has been passed to the variable.
  • C# Out Parameter Example

Let's consider a scenario to demonstrate the out parameter in C#.

Example

Example

using System;

class Program

{

    static void GetSquare(int num_ber, out int square)

    {

        square = num_ber * num_ber;

    }

    static void Main()

    {

        int input = 6;

        int result;

        GetSquare(input, out result);

        Console.WriteLine("The Square of " + input + " is: " + result);

    }

}

Output:

Output

The Square of 6 is: 36

Explanation:

In this instance, we are utilizing the out parameter to compute the square of a given number. Subsequently, a function called GetSquare is created, accepting an integer input and employing an out parameter to provide its square value. Within the main method, a number is initialized, and the GetSquare function is invoked. Ultimately, the square of the number is displayed using the Console.WriteLine method.

Important Points of Out parameter

Several important points of out parameter in C# are as follows:

  • It is similar to the ref keyword.
  • The data is passed in a unidirectional (out) manner.
  • Method overloading can also be done using an out parameter.
  • We cannot use properties as an out parameter because they are not actual variables that can be changed directly.
  • C# Out Parameter Example

Let's consider an example to demonstrate an out parameter in C#.

Example

Example

using System;

namespace OutParameterSimple

{

    class Program

    {

        // Create method using out parameter

        public void Value(out int number)

        {

            number = 10; // Assigning the value

        }

        static void Main(string[] args)

        {

            int result;

            Program program = new Program();

            program.Value(out result); // Calling method

            Console.WriteLine("The Value set by the method: " + result);

        }

    }

}

Output:

Output

The Value set by the method: 10

Explanation:

In this instance, we define a function named AssignValue which takes in a single out parameter named numeral. Within the function, we allocate the number 10 to this parameter. Within the primary function, we introduce a variable outcome without providing it with an initial value. Following that, we instantiate an instance of the program class and invoke the AssignValue function using the out keyword. This grants the function the ability to update the value of the result.

When to use an out parameter in C#?

In C# programming, the out parameter is employed when there is a need to retrieve multiple values from a method. For instance, let's consider extracting two numbers from a single method invocation.

Example

Example

using System;

class Program

{

    static void SumAndProduct(int p, int q, out int sum, out int product)

    {

        sum = p + q;

        product = p * q;

    }

    static void Main()

    {

        int number1 = 5, number2 = 10;

        int Sum, Product;

        SumAndProduct(number1, number2, out Sum, out Product);

        // Output the results

        Console.WriteLine($"Sum: {Sum}, Product: {Product}");

    }

}

Output:

Output

Sum: 15, Product: 50

Explanation:

In this instance, we define a function named CalculateTotal that takes two output parameters. Within this function, we determine the total sum and product of the given numbers. In the primary function, we specify that the values 5 and 10 are provided to the function, and retrieve their combined sum and product.

C# Multiple Out Parameters

In C#, the multiple out parameter enables the passing of multiple out parameters to a method, which in turn allows the method to return multiple values.

Syntax

It has the following syntax.

Example

return_type MethodName (datatype1 param1, datatype2 param2, ..., datatypeN paramN)

{

    // Method 

}
  • return_type: This element designates the method's data type.
  • datatype: It describes the specific data type that is being supplied to the method.

C# Multiple Out Parameter Example:

Let's consider a scenario to demonstrate the various parameters in C#.

Example

Example

using System;

class Program

{

    // Method

    static void Details(string name, int age, string city)

    {

        Console.WriteLine($"Name: {name}");

        Console.WriteLine($"Age: {age}");

        Console.WriteLine($"City: {city}");

    }

    static void Main()

    {

        // Calling method 

        Details("Alice", 25, "New York");

    }

}

Output:

Output

Name: Alice

Age: 25

City: New York

Explanation:

In this illustration, we define a function called DisplayInfo that takes in three arguments: a name, an age, and a location. Subsequently, we utilize the Console.WriteLine method to display these inputs. Following this, we invoke the function DisplayInfo within the main method while providing the arguments "Alice", 25, and "New York".

Difference between ref and out keyword

Several variances between the ref and out keywords are outlined below:

ref out
It is mandatory to initialize the parameter before it is passed to the ref. It is not necessary to initialize the parameter before it is passed to out.
It does not need to initialize the value of the parameter. It is necessary to initialize the value of the parameter.
The data is passed in a bi-directional(in/out). The data is passed in unidirectional(out).
It is used for modifying the existing value. It is used for returning a new value.

Conclusion

In summary, the use of the out keyword in C# enables functions to yield multiple results, proving to be highly beneficial in cases where a single return value is inadequate. Unlike the ref keyword, it eliminates the need for variable initialization prior to its usage.

C# out Parameter FAQs

1) What is an out Parameter in C#?

In C#, the out keyword is used to pass the arguments. It is like a reference-type, except it does not require a variable to initialize before passing. We must use the out keyword to pass arguments as an out-type. It is useful when we want a function to return multiple values.

2) Can a Method return multiple out parameters?

Yes, a method returns multiple out parameters.

Example

void Values(out int x, out int y) {

    x = 10;

    y = 20;

}

Yes, it is possible to utilize an out parameter in an asynchronous method in C# programming.

No, the out parameter is not compatible with an asynchronous method.

4) Is it possible to utilize an out parameter within the constructor parameter?

In C# development, constructors cannot have the out parameter as one of their parameters.

5) What is the syntax of an out parameter in C#?

It has the following syntax.

Example

Void MethodName (out Datatype parameter1, out Datatype parameter2)

Where,

  • void: It is the void type method, which means the method does not return a value directly.
  • DataType: It defines the type of data that has been passed to the method.
  • Parameter: It is used for holding the value that has been passed to the variable.

Input Required

This code uses input(). Please provide values below: