In this article we are going to discuss about Out and Ref parameters with examples in C#.
What is the difference between Out and Ref parameters?
ref -
The ref keyword indicates that a parameter is passed by reference and must be initialized before it is passed to the method.
public void ModifyRef(ref int number)
{
number = 10;
}
static void Main()
{
int a = 5;
ModifyRef(ref a);
Console.WriteLine(a); // Output: 10
}
out - The out keyword indicates that a parameter is passed by reference and does not need to be initialized before it is passed to the method. However, it must be assigned a value before the method returns.
public void ModifyOut(out int number)
{
number = 10;
}
static void Main()
{
int a;
ModifyOut(out a);
Console.WriteLine(a); // Output: 10
}
In C#, ref and out keywords serve different purposes, and their usage is guided by specific requirements and conditions. Below are scenarios where you can only use one of them and not the other:
Existing Variable Value Needs to Be Read and Modified
public void AddToExistingValue(ref int number, int valueToAdd)
{
number += valueToAdd; // Read and modify the existing value
}
static void Main()
{
int a = 10; // Must be initialized
AddToExistingValue(ref a, 5);
Console.WriteLine(a); // Output: 15
}
public void InitializeValue(out int number)
{
number = 10; // Must assign a value before returning
}
static void Main()
{
int a; // Does not need to be initialized
InitializeValue(out a);
Console.WriteLine(a); // Output: 10
}
In-Place Updates
public void Increment(ref int number)
{
number++; // Read and modify the current value
}
public void DoubleValue(ref int number)
{
number *= 2;
}
static void Main()
{
int a = 5; // Must be initialized
DoubleValue(ref a);
Console.WriteLine(a); // Output: 10
}
Return Multiple Values
public void GetCoordinates(out int x, out int y)
{
x = 10; // Assign value within the method
y = 20; // Assign value within the method
}
static void Main()
{
int x, y; // No need to initialize
GetCoordinates(out x, out y);
Console.WriteLine($"x: {x}, y: {y}"); // Output: x: 10, y: 20
}
public bool TryParseNumber(string input, out int result)
{
return int.TryParse(input, out result); // Assign value within the method if parsing is successful
}
static void Main()
{
if (TryParseNumber("123", out int number))
{
Console.WriteLine($"Parsed number: {number}"); // Output: Parsed number: 123
}
else
{
Console.WriteLine("Parsing failed.");
}
}