“Ref” and “Out” are the parameter modifiers used in C#. Using ref and out, you can pass any value type by its reference to any method. The reason of passing any value type by its reference introduces the main difference between the ref and out keyword.
The ref keyword allows the called method to alter the content of the argument passed to it with ref keyword. The out keyword allows called method to return more than one value at a single call. Let’s study the difference between ref and out along with a comparison chart.
Content: Ref Vs Out in C#
Comparison Chart
Basis for Comparison | Ref | Out |
---|---|---|
Basic | It allows the alteration in the value of argument proceeded with ref keyword. | It allows a method to return those values preceded by an out keyword. |
Initialization | The ref parameter must be initialized prior to the method call. | The out parameter must be initialized inside the called method before it terminates. |
Declaration | The parameter to be altered by a method is declared as ref while method declaration and method calling. | The parameter to be returned must be declared as ref while method declaration and method calling. |
Definition of Ref Keyword
The “ref” is a parameter modifier used when there is a need of calling a method with call-by-reference. When we want that the changes made to the arguments inside a called method must reflect in the original value of that variable, then the ref parameter modifier is used.
The value type which we want to declare as a ref is preceded by the “ref” keyword during method declaration, and method calling. More than one parameter can be declared as a “ref” while calling and declaring a method.
using System; class DemoRef { public void Square(ref int s) { s = s * s; } } class Ref_main{ static void Main() { DemoRef ob = new DemoRef(); int a = 10; Console.WriteLine("a before method call: " + a); ob.Square(ref a); // Used ref Keyword Console.WriteLine("a after call: " + a); } } //output a before method call: 10 a after method call: 100
In above code, the variable ‘a’ is passed as an argument to the method Square(ref a) along with the parameter modifier ref attached to it. It means whatever changes the method Square(ref a) will perform on the variable ‘a’ inside it will reflect in original value of the ‘a’, outside the method also.
There are some important points which must be remembered when dealing with parameter modifier “ref”.
- The argument you are passing by the ref to a method must be initialized prior to the method calling.
- The method must not assign the initial value to the ref argument.
- You can also use ref along with the reference variable also.
Definition of Out Keyword
Each time you call a method, it would return a single value only. If you want a method to return more than one value at a call, the “out” keyword must be used along with the parameters which you want to return back when the method is terminated.
Sometimes it is the case that when we do not want to pass anything to the method but want the method to return back something we use parameters with an out keyword. Let’s understand it with an example.
using System; class DemoOut{ public int Decompose( double x, out double fraction) { int whole_num; whole_num = (int) x; fraction = x - whole_num; return whole_num; } } class Out_maint { static void Main() { DemoOut ob = new DemoOut(); int i; double frac; i = ob.Decompose(100.125, out frac); //used out keyword Console.WriteLine("Whole number part is " + i); Console.WriteLine("fraction part is " + frac); } } // output Integer portion is 100 Fractional part is 0.125
In above code, two values are being returned by the, method Decompose( ). One it returns by the keyword “return” i.e. whole_num, and other it returns a parameter which is preceded by the out keyword while method calling i.e. “frac”.
The important points to remember about the out keyword.
- Unlike ref keyword, the parameter using out keyword must not be initialized prior to method calling.
- The called method will itself assign a value to the parameter with an out keyword as it is considered as unassigned inside the called method i.e. it is assumed to have no initial value.
- The called method must assign a value to the out parameter before the method terminates.
- The out keyword is also applicable to the reference variable.
Key Differences Between Ref and Out in C#
- 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. When a variable passed to a method is preceded by out keyword the method returns it without using return keyword.
- The ref parameter should be initialized before it is passed to a method. On the other hand, the out parameter must be initialized inside the method it is passed to.
- While calling, declaring, or defining a method, ref parameter is explicitly declared as ref. On the other hand, while calling, declaring, or defining a method, out parameter is explicitly declared as out.
Conclusion
Whenever the variable is to be passed by the reference to a method the ref and out keyword can be used. Whereas, the reason of using both the keywords are different where the ref keyword can be used to alter the value of the argument preceded by ref keyword, passed to the called method and the out keyword is used to return back the value of the argument preceded by the out keyword.
Leave a Reply