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#