In this article, you’ll learn what is the difference between the ref and out parameters in c#.
ref and out keywords in C# are used to pass arguments within a method. By default, parameters are passed to a method by value. By using this ref or out keywords you can pass a parameter by reference.
The ref keyword passes arguments by reference. It means any changes made to this argument in the method will be reflected in that variable when the control returns to the calling method.
In Simple Words, “ref is used to state that the parameter passed may be modified by the method.”
The ref requires the parameter to have been initialized before being passed to a method.
Let’s take an example:
public class Program {
static void Main(string[] args) {
var name = "tutorialsrack";
var value = abc(ref name);
Console.WriteLine("Ref Output: " + value);
// Ref Output: tutorialsrack
Console.ReadKey();
}
public static string abc(ref string name) {
return name;
}
}
Using the ref keyword, you can also change value types outside the method as well. Let’s take an example:
public class Program
{
static void Main(string[] args) {
var name = "tutorialsrack";
var value = abc(ref name);
Console.WriteLine("Ref Output: " + value);
// Ref Output: hello, tutorialsrack
Console.ReadKey();
}
public static string abc(ref string name) {
return $ "hello, {name}";
}
}
The out keyword passes arguments by reference. out keyword is very similar to the ref keyword except that ref requires that the variable be initialized before it is passed.
In simple words, “out is used to state that the parameter passed must be modified by the method.”
The out modifier does not require this and is typically not initialized prior to being used in a method.
Let’s take an example:
public class Program {
static void Main(string[] args) {
string name;
var value = xyz(out name);
Console.WriteLine("Out Parameter Output: " + value);
// Out Parameter Output: tutorialsrack
Console.ReadKey();
}
public static string xyz(out string name) {
name = "tutorialsrack";
return name;
}
}
out and ref cannot be used in methods with the async modifier.yield return or yield break either.Both ref and out parameters are treated the same at compile-time but different at run-time, so methods cannot be overloaded if one method takes an argument as ref and the other method takes an argument as an out.
Let’s take an example:
static void Method1(ref int i)
{
}
static void Method1(out int i)
{
}
|
ref keyword |
out keyword |
|
It is necessary the parameters should initialize before it passes to ref. |
It is not necessary to initialize parameters before it passes to out. |
|
It is not necessary to initialize the value of a parameter before returning to the calling method. |
It is necessary to initialize the value of a parameter before returning to the calling method. |
|
The passing of value through |
The declaring of parameters throughout parameters is useful when a method returns multiple values. |
|
When the |
When |
1. The ref modifier means that:
2. The out modifier means that:
3. The ref requires the parameter to have been initialized before being passed to a method. Whereas, the out does not require to be initialized before being passed to a method.
4. Both ref and out parameters are treated the same at compile-time but different at run-time.
5. Properties are not variables, therefore it cannot be passed as an out or ref parameter.
I hope this article will help you to understand what is the difference between the ref and out parameters in c#.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments