In this article we will see what is the Difference between Ref and Out keywords in C#
Here is the simple example to show you the difference between Ref and Out in C#
Below are some differences:
Ref:
Out:
Here is the simple example to show you the difference between Ref and Out in C#
class Program
{
static void Main(string[] args) //calling method
{
int valref = 0; //must be initialized
int valout; //not compulsary
method1(ref valref);
Console.WriteLine(valref);
method2(out valout);
Console.WriteLine(valout);
Console.ReadLine();
}
static void method1(ref int value) //called method
{
value = 1; //not compulsary to initialize
}
static void method2(out int value) //called method
{
value = 2; //must be initialized
}
}
Below are some differences:
Ref:
- Must initialized before passing to its called method
- This is bi-directional
- The value of the ref parameter can be changed before passing to its calling method.
Out:
- Not compulsory to initialize before passing to the method
- This is uni-directional
- The value initialized for the Out parameter in the called method will be returned to its calling method.
Difference between Ref and Out keywords in C#
In this article we will see what is the Difference between Ref and Out keywords in C# Here is the simple example to show you the differen...
