;

Difference between ref and out Parameter in C#


Tutorialsrack 29/01/2022 C#

In this article, you’ll learn what is the difference between the ref and out parameters in c#.

ref and Out parameter 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.

ref Keyword in C#

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}";
   }
}

out Keyword in C#

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;
   }
}

Modifiers Are Not Allowed on All Methods

  • The out and ref cannot be used in methods with the async modifier.
  • You cannot use them in iterator methods that have a yield return or yield break either.

Ref and Out keyword and method Overloading

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 vs out keyword in C#

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 ref parameter is useful when the called method also needs to modify the value of the passed parameter.

The declaring of parameters throughout parameters is useful when a method returns multiple values.

When the ref keyword is used the data may pass in bi-directional.

When out keyword is used the data only passes in unidirectional.

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!


Related Posts



Comments

Recent Posts
Tags