In C#, the out keyword is used to pass arguments to methods by reference instead of by value. This allows the method to change the value of the argument and that change is reflected outside the method. It is especially useful when you need a way to return multiple values.
Here are some examples to illustrate the use of the out keyword,
Key Points
Parameter Declaration-
The out keyword must be specified in the method declaration and method calls.
The parameter passed out does not need to be initialized before it is passed to the method, but a value must be provided in the method before it returns.
Method Signature-
The method signature includes the out keyword for any parameters to be passed by reference.
Multiple Return Values-
out parameters are often used to return multiple values from a method.
Using out to return multiple values
using System;
// Define a delegate
public delegate void MyDelegate(string message);
public class Program
{
public static void Main()
{
bool IsDivide = DivideNumber(10, 2, out int Result);
if(IsDivide)
{
Console.WriteLine("Result is: "+ Result);
}
else
{
Console.WriteLine("Division failed: Please enter valid number");
}
static bool DivideNumber(int dividend, int divisor, out int result)
{
if(divisor ==0)
{
result =0;
return false;
}
else
{
result = dividend/divisor;
return true;
}
}
}
}
Output-
Result is: 5
If enter divisor value 0 (zero)
Division failed: Please enter valid number
Using out with TryParse
using System;
// Define a delegate
public delegate void MyDelegate(string message);
public class Program
{
public static void Main()
{
Console.WriteLine("Enter string for convert into number");
string str = Console.ReadLine();
// Verify the entered value is valid or not
if(int.TryParse(str, out int Result))
{
Console.WriteLine("Number is: "+ Result);
}
else
{
Console.WriteLine("Conversion failed: enter valid format");
}
}
}
Output 1-
Enter string for convert into number
1524
Number is: 1524
Output 2-
Enter string for convert into number
1234abc
Conversion failed: enter valid format
The out keyword provides a way to return multiple values from a single method, increasing the simplicity and efficiency of your method in C#.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
C# out Keyword
In C#, the out keyword is used to pass arguments to methods by reference instead of by value. This allows the method to change the value of the argument and that change is reflected outside the method. It is especially useful when you need a way to return multiple values.
Here are some examples to illustrate the use of the out keyword,
Key Points
Parameter Declaration-
outkeyword must be specified in the method declaration and method calls.outdoes not need to be initialized before it is passed to the method, but a value must be provided in the method before it returns.Method Signature-
outkeyword for any parameters to be passed by reference.Multiple Return Values-
outparameters are often used to return multiple values from a method.Using
outto return multiple valuesOutput-
If enter divisor value 0 (zero)
Using
outwithTryParseOutput 1-
Output 2-
The out keyword provides a way to return multiple values from a single method, increasing the simplicity and efficiency of your method in C#.
Also Read: How to convert string into int in C#?