| Explain out keyword in c#
When we talk about out keyword in c# it is basicaly used to pass arguments to method as a reference type. The out parameter in c# is useful to return more than one value from the methods in c#. We can use out keyword with variables and method parameters. Out parameter is always passed by reference for both value type and reference type data types.
Additional Features
Below are the 3 more features of c# out parameter
(1)We will no need to initialize parameters before it pass to out.
(2)We need to initialize the value of a parameter before returning to the calling method.
(3)It is not necessary that Out parameter name should be same in both function definition and call.
Declare of c# Parameter
Below is the simple example of using out parameters in c# language
int x,y;
multiply(out x, out y);
As in above declartion we have declared a variable x,y and pass it to method by using out parameter but not assign any value . And value is be initialized in called method before it return a value in calling method. Both the method definition and the calling method must explicitly use the out keyword.
Complete Example of out keyword
using System;
namespace Testoutkeyword
{
class Program
{
static void Main(string[] args)
{
int x, y;
multiply(out x, out y);
Console.WriteLine("Value of x : {0}", x);
Console.WriteLine("Value of y : {0}", y);
Console.ReadLine();
}
public static void multiply(out int a, out int b)
{
a = 20;
b = 10;
a *= a;
b *= b;
}
}
}
OutPut of Above is
Value of X : 400
Value of Y : 100 | | |