In this blog, I am going to explain the difference between Ref and Out Keywords in C#. This detailed blog will cover the following topics as follows
- Introduction
- Reference Parameters in C#
- What is the ref in C#?
- Key Points
- What is out in C#?
- Key Points
- Bonus Concept
- Method Overloading with Ref and Out Keywords
- Similarities between Ref and Out keywords in C#
- Difference between Ref and Out keywords in C#
- Conclusion
Reference Parameters in C#
As per Microsoft, "You apply one of the following modifiers to a parameter declaration to pass arguments by reference instead of by value":
- ref: The argument must be initialized before calling the method. The method can assign a new value to the parameter but isn't required to do so.
- out: The calling method isn't required to initialize the argument before calling the method. The method must assign a value to the parameter.
- ref readonly: The argument must be initialized before calling the method. The method can't assign a new value to the parameter.
- in: The argument must be initialized before calling the method. The method can't assign a new value to the parameter. The compiler might create a temporary variable to hold a copy of the argument to
inparameters.
Ref Keyword in C#
The ref keyword in C# is used to pass arguments by reference. In simple words, any changes made to this argument in the method will be reflected in that variable when the control returns back to the calling method.
Key Points
- An argument for a ref parameter must include the ref modifier/keyword.
- Data can pass bi-directionally when the ref keyword is used.
- Ref means the method can read or write the value of the argument.
- Properties cannot be arguments to ref parameters since they are variables, not methods.
Examples
The example given in this section demonstrates the functionality of Ref Keyword in C#. Let's see.
namespace RefVsOutInCsharp.RefKeyword
{
internal class RefInCsharp
{
static void Main(string[] args)
{
int originalValue = 10;
Console.WriteLine("-- Example of Ref Keyword In C# --");
Console.WriteLine("Original Ref value: " + originalValue);
DoubleValue(ref originalValue);
Console.WriteLine("New Ref value: " + originalValue);
Console.ReadLine();
}
static void DoubleValue(ref int value)
{
value *= 2;
}
}
}
Output

Out Keyword in C#
Out keyword in C# is used to pass arguments by reference. Declaring parameters for out method is useful when multiple values need to be returned from the method.
Key Points
- An argument for an out parameter must include the out modifier/keyword.
- Out means the method sets the value of the argument.
- When out keyword is used, then data is passed in a unidirectional way only.
- Properties cannot be arguments to out parameters since they are variables, not methods.
Examples
The example given in this section demonstrates the functionality of Out Keyword in C#. Let's see.
namespace RefVsOutInCsharp.OutKeyword
{
internal class OutInCsharp
{
static void Main(string[] args)
{
int area, perimeter;
CalculateDimensions(100, 200, out area, out perimeter);
Console.WriteLine("-- Example of Out Keyword In C# --");
Console.WriteLine("Area: " + area);
Console.WriteLine("Perimeter: " + perimeter);
Console.ReadLine();
}
static void CalculateDimensions(int width, int height, out int area, out int perimeter)
{
area = width * height;
perimeter = 2 * (width + height);
}
}
}
Output

Bonus Concept: Method Overloading with Ref and Out Keywords
According to Microsoft, “Member overloading means creating two or more members on the same type that differ only in the number or type of parameters but have the same name”. But, when you apply method overloading with ref and out keywords, it will behave differently. As both ref and out keywords are treated differently at run time and at compile time, they are treated the same.
Example
namespace RefVsOutInCsharp.MethodOverloadingWithRefAndOut
{
class MethodOverloadingWithRefAndOut
{
public static void Main(string[] args)
{
int id = 0;
MethodOverloadingWithRefAndOutInCSharp(ref id);
MethodOverloadingWithRefAndOutInCSharp(out id);
}
public static string MethodOverloadingWithRefAndOutInCSharp(ref int id)
{
Console.WriteLine(id);
id += 1;
return "Ref";
}
public static string MethodOverloadingWithRefAndOutInCSharp(out int id)
{
id = 0;
Console.WriteLine(id);
return "Out";
}
}
}


Join the conversation! Your thoughts help the community grow.