Ref and out keywords in C# are used to pass arguments within a method or function. Both indicate that an argument/parameter is passed by reference. By default, parameters are passed to a method by value. By using these keywords (ref and out) we can pass a parameter by reference.
Ref Keyword
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.
Example code
public static string GetNextName(ref int id)
{
string returnText = "Next-" + id.ToString();
id += 1;
return returnText;
}
static void Main(string[] args)
{
int i = 1;
Console.WriteLine("Previous value of integer i:" + i.ToString());
string test = GetNextName(ref i);
Console.WriteLine("Current value of integer i:" + i.ToString());
}
Output

Out Keyword
The out keyword passes arguments by reference. This is very similar to the ref keyword.
Example Code
public static string GetNextNameByOut(out int id)
{
id = 1;
string returnText = "Next-" + id.ToString();
return returnText;
}
static void Main(string[] args)
{
int i = 0;
Console.WriteLine("Previous value of integer i:" + i.ToString());
string test = GetNextNameByOut(out i);
Console.WriteLine("Current value of integer i:" + i.ToString());
}
Output
Ref Vs Out
| Ref |
Out |
| The parameter or argument must be initialized first before it is passed to ref. |
It is not compulsory to initialize a parameter or argument before it is passed to an out. |
| It is not required to assign or initialize the value of a parameter (which is passed by ref) before returning to the calling method. |
A called method is required to assign or initialize a value of a parameter (which is passed to an out) before returning to the calling method. |
| Passing a parameter value by Ref is useful when the called method is also needed to modify the passed parameter. |
Declaring a parameter to an out method is useful when multiple values need to be returned from a function or method. |
| It is not compulsory to initialize a parameter value before using it in a calling method. |
A parameter value must be initialized within the calling method before its use. |
| When we use REF, data can be passed bi-directionally. |
When we use OUT data is passed only in a unidirectional way (from the called method to the caller method). |
| Both ref and out are treated differently at run time and they are treated the same at compile time. |
| Properties are not variables, therefore they cannot be passed as an out or ref parameter. |
Ref / Out keyword and method Overloading
Both ref and out are treated differently at run time and they are treated the same at compile time, so methods cannot be overloaded if one method takes an argument as ref and the other takes an argument as an out.
Example code
public static string GetNextName(ref int id)
{
string returnText = "Next-" + id.ToString();
id += 1;
return returnText;
}
public static string GetNextName(out int id)
{
id = 1;
string returnText = "Next-" + id.ToString();
return returnText;
}
Output when the code is compiled.

However, method overloading can be possible when one method takes a ref or out argument and the other takes the same argument without ref or out.
Example Code
public static string GetNextName(int id)
{
string returnText = "Next-" + id.ToString();
id += 1;
return returnText;
}
public static string GetNextName(ref int id)
{
string returnText = "Next-" + id.ToString();
id += 1;
return returnText;
}
Summary
The out and ref keywords are useful when we want to return a value in the same variables that are passed as an argument.
Dheerendra DwivediPosted Jul 11, 2023, 6:15 PM
Samajh me na aaya kuch .
GerardPosted Jan 16, 2021, 10:54 AM
"Both ref and out are treated differently at run time and they are treated the same at compile time." I think you've got this backwards, they are treated the same at runtime, they are treated differently at compile time! The main differences between ref and out is that with out the compiler requires you to initialize the parameter within the method before you use it, otherwise you'll get CS0269, and if you don't assign anything you'll get CS0177. With ref the compiler requires you to pass an initialized variable, otherwise you'll get CS0165. In the debugger you can see that an out parameter has the value you pass to it before you initialize it it in the method, it is only the compiler that doesn't allow you to use it.
harry zPosted Sep 25, 2020, 4:42 PM
Ref tells the compiler that the object is initialized before entering the function, while out tells the compiler that the object will be initialized inside the function.
Abhijeet ChoudhariPosted Jul 12, 2020, 1:42 PM
Nice article.I have one question : if we have one function then which one we should use preferably ref or out...!! And why ?
Abhay SharmaPosted May 23, 2020, 1:51 PM
Good explanation.
Rikam PalkarPosted May 12, 2020, 8:31 AM
Nicely done. I still have doubt, why do I need to initialise out parameter inside the method, what purpose does it serves?
Arshdeep KaurPosted Apr 10, 2020, 12:33 AM
I believe most important use case of out parameter is to return multiple values from a single called method. Whereas ,In normal function call ( without Out parameter), We can return only one value. Example from Microsoft documentation will make the concept more clear to you: void Method (out int answer, out string message, out string stillNull) { answer = 44; message = "I've been returned"; stillNull = null; } int argNumber; string argMessage, argDefault; Method(out argNumber, out argMessage, out argDefault); Console.WriteLine(argNumber); Console.WriteLine(argMessage); Console.WriteLine(argDefault == null); // The example displays the following output: // 44 // I've been returned // True Notice that we returned multiple values from Method with different data types. This use case is widely used.
chethan kumarPosted Oct 16, 2019, 2:28 AM
Better Example to understand Ref and out using System; public class Program { public static void Main() { int i; // we need not initialize for out int j=20; string test = GetNextNameByOut(out i); string testref = GetNextNameByRef(ref j); Console.WriteLine("Current value of integer i:" + i.ToString()); // Current value of integer i:10 Console.WriteLine("Current value of integer i:" + test); //Current value of integer i:Next-10 Console.WriteLine("Return value of out " + j.ToString()); //Return value of out 20 Console.WriteLine("Return value of ref " + testref);//Return value of ref Next-20 } public static string GetNextNameByOut(out int id) { id = 10; //complusary initialization before using string returnText = "Next-" + id.ToString(); return returnText; } public static string GetNextNameByRef(ref int id) { string returnText = "Next-" + id.ToString(); return returnText; } }
mohammed hussainPosted Apr 30, 2019, 1:13 AM
A real time example please. why and when we use these keywords.
jitendra jayswalPosted Feb 12, 2019, 12:07 AM
In case of ref
jitendra jayswalPosted Feb 12, 2019, 12:06 AM
Wrong (It is not compulsory to initialize a parameter value before using it in a calling method.)
Raj SharmaPosted May 17, 2018, 3:04 AM
Excellent explanation..
Rajenthiran TPosted Apr 25, 2018, 11:08 AM
Public static string GetNextNameByOut(out int id) { id = 1; string returnText = "Next-" + id.ToString(); return returnText; } void Main() { int i = 8; string test = GetNextNameByOut(out i); } In following code 'Out' code is calling from main function to calling method GetNextNameByOut(out int id){ id=1 you initialized. when ever we passing any value for it (out i) id value changed into 1, then why we have to call out key word? Please give assist..
x xPosted Feb 1, 2018, 4:36 AM
Great article. Thank you very much. keep it up
Upendra Pratap ShahiPosted Nov 21, 2015, 3:05 AM
nice sir
Ravi PatelPosted Nov 10, 2015, 10:47 PM
nice explained
Lalit RaghavPosted Oct 18, 2015, 2:03 PM
nice article