Difference Between Ref And Out Keyword In C#

SHARE

ref in C#

The ref keyword in C# is used for passing or returning references of values to or from Methods. Basically, it means that any change made to a value that is passed by reference will reflect this change since you are modifying the value at the address and not just the value. It can be implemented in the following cases:

  • To pass an argument to a method by its reference.
  • To define a method signature to return a reference of the variable.
  • To declare a struct as a ref struct
  • As local reference

What you need to know about ref Keyword

  • When a variable preceded by the ref keyword is passed to any method then the changes made to it inside the method reflects in its original value. 
  • Ref keyword is used when a called method has to update the passed parameter.
  • The passing of value through ref parameter is useful when the called method also need to change the value of passed parameter.
  • When ref keyword is used the data may pass in bi-directional.
  • It is not necessary to initialize the value of a parameter before returning to the calling method.
  • The ref parameter should be initialized before it is passed to a method. 
  • While calling, declaring, or defining a method, ref parameter is explicitly declared as ref.

out Keyword in C#

The out is a keyword in C# which is used for the passing the arguments to methods as a reference type. It is generally used when a method returns multiple values. It makes the formal parameter an alias for the argument, which must be a variable. In other words, any operation on the parameter is made on the argument. It is like the ref keyword, except that ref requires that the variable be initialized before it is passed.

It is also like the in keyword, except that in does not allow the called method to modify the argument value. To use an out parameter, both the method definition and the calling method must explicitly use the out keyword.

What you need to know about Out Keyword

  • When a variable passed to a method is preceded by out keyword the method returns it without using return keyword.
  • Out keyword is used when a called method has to update multiple parameter passed.
  • The declaring of parameter through out parameter is useful when a method return multiple values.
  • When out keyword is used the data only passed in unidirectional.
  • It is necessary to initialize the value of a parameter before returning to the calling method.
  • The out parameter must be initialized inside the method it is passed to.
  • While calling, declaring, or defining a method, out parameter is explicitly declared as out.

Also Read: Difference Between Anaconda And Python Programming

Ref vs Out Keyword In Tabular Form

Basis of ComparisonRef KeywordOut Keyword
DescriptionWhen a variable preceded by the ref keyword is passed to any method then the changes made to it inside the method reflects in its original value. When a variable passed to a method is preceded by out keyword the method returns it without using return keyword.
UseRef keyword is used when a called method has to update the passed parameter.Out keyword is used when a called method has to update multiple parameter passed.
ParameterThe passing of value through ref parameter is useful when the called method also need to change the value of passed parameter.The declaring of parameter through out parameter is useful when a method return multiple values.
DataWhen ref keyword is used the data may pass in bi-directional.When out keyword is used the data only passed in unidirectional.
Value of parameterIt 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.
Outer ParameterThe ref parameter should be initialized before it is passed to a method. The out parameter must be initialized inside the method it is passed to.
CallingWhile calling, declaring, or defining a method, ref parameter is explicitly declared as ref.While calling, declaring, or defining a method, out parameter is explicitly declared as out.

Also Read: Difference Between Int And long