Ref and Out in C#

 Ref in c#

The ref is keyword that describes that any value that passes through the reference. We can use the ref keyword in 4 ways;

  • In a programming code, in that method block or the method call, by that we can pass any argument to that particular method through the reference.
  • To that method we can returns any value to call the method by the reference.
  • In the functional body, we can notice that the reference values are basically stored in the local by the reference that can be altered.
  • In the functional body we can declare the struct function or to declare the ref struct function or to declare the readonly ref struct function.

Let's see a simple program using ref keyword

Example 1:

//C# program to define the ref keyword in the program
// use of ref keyword
using System;    //  objectname


namespace refkeyword
{
class JTP {
static void Main(string[] args)  // main method
{
int x = 19, y = 11;   assigning value x and y
Console.WriteLine("The first Num of x is {0}", x);   // printing the first values
Console.WriteLine(" The first Num of y is {0}", y);  // printing y values
Console.WriteLine();
addNum(x);   // adding by ref
Console.WriteLine(" The Number of x after addition of modification"+
" operation is {0}", x);
subtractNum(ref y);  // subtraction by ref
Console.WriteLine("The Number of y after subtraction of modification "+
"operation is {0}", y);
}
public static void addNum(int x)  // showing add values by ref
{
x += 19;
}
public static void subtractNum(ref int y) // showing subtracting values by ref
{
y -= 11;
}
}
}

Output:

Ref and Out in C#

Out in c#

The out is one of the keywords in C#. This is also like the ref keyword. Out keyword is a keyword that describes that any value that passes through the reference type. This is used for when a method that returns the more than one values (I.e. Multiple values)

The following ways that are used for the Out keywords as:

  • This is same as the ref keyword. The only difference between these both keywords are that ref keyword initialize the values before it throws to the method. Whereas in the out keyword does not initialize the values before it throws the variable into the method.
  • It is as same as the keyword that does not allow the method to change the values but the ref keyword allows the method to change.
  • By using the out keyword, we must notice that method callings and the method definitions are defined the out keyword explicit.
  • We can have more than one out parameters in the method which we allow to change.
  • The out parameter does not allow the iterator methods and asynchronous methods.

Let us see a simple program using the out-keyword

Example 2:

// C# program that defines the out keyword
using System;
class JTP  // object name
{
static public void Main()  // main method
{
int a;  // initializing a value
Addition(out a); // passing a value to the out parameter
Console.WriteLine("The value after performing addition operation is: {0}", a);
} // printing a values
public static void Addition(out int a) // the out parameter is in method which passes the method
{
a = 10;
a += a;
}
}

Output:

Ref and Out in C#

Let's see another program using multiple out parameters:

Example 3:

// C# program to define the multiple out parameters
using System;
class JTP
{
static public void Main() // main method
{
		int x, y; // initializing x, y values without assigning the vales to it.
Addition(out x, out y); // pass multiple variables to the method Console.WriteLine("The value after performing the addition operation is: {0}", x);
Console.WriteLine("The value after performing the addition operation is: {0}", y);
} // printing x and y values
	public static void Addition(out int a, out int b)
{
a = 10;
b = 20;
a += a;
b += b;
}
}

Output:

Ref and Out in C#

Difference between Ref and Out in C#

Let us see the difference between ref and out in c#

ref keywordout keyword
We have to initialize the parameters or the arguments before it passes through the ref by methodIs not must necessary that the parameters before it passes through the out by method
It is not important that the values of the arguments or the parameters before returns the calling function or method.It is important that the values of the arguments or the parameters before returns the calling function or method.
The moving of the value from one method to another through the ref parameter that is important when calling the method to change the value of that parameter passed.The initializing of parameters by the out parameter that is useful for the method which returns the multiple values.
The ref keyword that is useful for the data which passes in bi-directional.The out keyword is useful for the data which passes in the unidirectional.