Table of contents
1.
Introduction
2.
Out parameter in C#
2.1.
Declaration
2.1.1.
Example
2.2.
Multiple out Parameters
2.2.1.
Example
2.3.
Improvements to the Out Parameter in C# 7.0
2.3.1.
Example
3.
Frequently Asked Questions
3.1.
What is the out modifier in C#?
3.2.
Do you need to declare an out variable before using it C#?
3.3.
What is the difference between out and ref parameters in C#?
4.
Conclusion
Last Updated: Mar 27, 2024

C# Out Parameter

Author Sanjana Yadav
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

This article describes the new features of the out parameter in C# 7.0. When a method returns several values, the C# out parameter is used. When a parameter is passed to the method using the Out keyword/parameter, the method uses the same variable value that was passed in the method call. When the value of a variable change, so does the value of a method argument.

Out parameter in C#

The out keyword is a C# keyword used to pass arguments to methods as a reference type. When a method returns multiple values, it is commonly used.

  • It's comparable to the “ref” keyword. However, the fundamental distinction between “ref” and “out” is that “ref” requires the variable to be created before being supplied to the function. However, before passing the parameter to the method, the variables do not need to be initialized. The variable must be initialized in the called method before it returns a value to the calling method.
  • It's identical to the “in”  keyword, except that the “in” keyword prevents the calling method from changing the parameter value, whereas the ref does.
  • When using it as a parameter, the “out” keyword must be used explicitly in both the method declaration and the calling method.
  • Asynchronous methods are not permitted to use the out parameters.
  • In iterator methods, out arguments are not permitted.
  • In a method, there might be several out parameters.
  • Out argument can be declared inline at the moment of method invocation. On the other hand, the inline out arguments can be retrieved in the same code block as the call.
  • Out arguments can also be used to overload methods.
  • Because properties are not variables, they cannot be passed as arguments.

Until C# 6.0, the variable had to be declared first, and then it could only be sent as an out parameter. However, beginning with C# 7.0, the user can define the out variable in the method call's argument list instead of a separate variable declaration.

Declaration

data_type variable_name;
method_name(out variable_name);

From C# 7.0 onwards, you may transform both of the above two lines of code as follows.

method_name(out data_type variable_name);

Before the called method delivers a value, the value of the variable_name must be initialized.

Example

using System;
class OutExample{
    static public void Main()
    {
        int x;
        // Pass variable to the method
        // using out keyword
        Multiply(out x);
        // Display the value x
        Console.WriteLine("The multiplication of the value is:{0}", x);
    }


    // Method in which out parameter is passed
    // and this method returns the value of
    // the passed parameter
    public static void Multiply(out int x)
    {
        x = 10;
        x *= x;
    }
}

Output

The multiplication of the value is:100 

Multiple out Parameters

In the code above, we defined a variable without initializing it, namely x.

Now we use the out keyword to give such parameters to the Multiply function, as in Multiply(out x, out y);.

The variables' values are assigned in the method to which they were supplied.

Example

using System;
class OutExample{
    static public void Main()
    {
        int x, y;
        // Pass multiple variable to
        // the method using out keyword
        square(out x, out y);
        Console.WriteLine("The square of the value is:{0}", x);
        Console.WriteLine("The square of the value is:{0}", y);
    }
    // Method in which out parameters
    // are passed, and this method returns
    // the values of the passed parameters
    public static void square(out int a, out int b)
    {
        a = 10;
        b = 20;
        a *= a;
        b *= b;
    }
}

Output

The square of the value is: 100
The square of the value is: 400

Improvements to the Out Parameter in C# 7.0

  • The out parameter in C# 7.0 can be sent without being declared and initialized, known as the in-line declaration of the Out parameter or Implicit Type Out Parameter. Its scope is restricted to the method body, i.e., it has a local scope.
  • In the method parameter list, the out parameter may use the var type.
  • It is not required that the name of the out parameter be the same in both definition and call.
  • It is also useful in the Try Pattern.

Example

using System;
class OutExample
{
    static public void Main()
    {
        // In-line declaring variables
        // without assigning values
        // Passing multiple variable to
        // the method using out keyword
        VolOfCuboid(out int l, out int w, out int h, out int volume);
        System.Console.WriteLine("Length of the cuboid is: " + l);
        System.Console.WriteLine("Width of the cuboid is: " + w);
        System.Console.WriteLine("Height of the cuboid is: " + h);
        System.Console.WriteLine("Volume of the cuboid is: " + volume);
        Console.ReadLine();
    }
    // Method in which out parameters are passed
    // and this method returns the values of
    // the passed parameters
    public static void VolOfCuboid(out int x, out int y, out int z, out int volume)
    {
        x = 3;
        y = 4;
        z = 2;
        volume = x * y * z;
    }
}

Output

Length of the cuboid  is:3
Width of the cuboid is:4
Height of the cuboid  is:2
Volume of the cuboid  is: 24

Also Read About - singleton design pattern in c#

Frequently Asked Questions

What is the out modifier in C#?

The out keyword can be used in two situations: As a parameter modifier, it allows you to send a method argument by reference rather than by value. This states that a type parameter is covariant in generic type parameter declarations for interfaces and delegates.

Do you need to declare an out variable before using it C#?

In C# 6 and earlier, you must define a variable in a separate statement before passing it as an out parameter.

What is the difference between out and ref parameters in C#?

Out is a keyword in C# that is used to send arguments to methods as a reference type. When a method returns multiple values, it is commonly used. The out parameter does not pass the property. In C#, the ref keyword sends arguments through a reference.

 

Read Also -  Difference between argument and parameter

Conclusion

In this article, we have extensively discussed the out parameter in C# with examples.

We hope that this blog has helped you enhance your knowledge of the parameters in C#, and if you would like to learn more about C#, check out our articles C# | Learn & Practice from Coding Ninjas Studio and C# interview questions for 5 years Experience.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass