Ref and out parameters are used to pass an argument within a method. In this article, you will learn the differences between these two parameters. Ref and out change the behavior of method parameters. Sometimes we want the actual value of a variable to be copied as the parameter. Other times we want a reference. These modifiers affect definite assignment analysis.
Ref
The ref keyword is used to pass an argument as a reference. This means that when value of that parameter is changed in the method, it gets reflected in the calling method. An argument that is passed using a ref keyword must be initialized in the calling method before it is passed to the called method.
Out
The out keyword is also used to pass an argument like ref keyword, but the argument can be passed without assigning any value to it. An argument that is passed using an out keyword must be initialized in the called method before it returns back to calling method.
Example using ref and out:
Output:
Is cat
dog
one
Ref and out in method overloading
Both ref and out cannot be used in method overloading simultaneously. However, ref and out are treated differently at run-time but they are treated same at compile time (CLR doesn't differentiates between the two while it created IL for ref and out). Hence methods cannot be overloaded when one method takes a ref parameter and other method takes an out parameter. The following two methods are identical in terms of compilation.
However, method overloading can be done, if one method takes a ref or out argument and the other method takes simple argument. The following example is perfectly valid to be overloaded.
Note:
The out and ref keywords are useful when we want to return a value in the same variables as are passed as an argument.
Ref
The ref keyword is used to pass an argument as a reference. This means that when value of that parameter is changed in the method, it gets reflected in the calling method. An argument that is passed using a ref keyword must be initialized in the calling method before it is passed to the called method.
Out
The out keyword is also used to pass an argument like ref keyword, but the argument can be passed without assigning any value to it. An argument that is passed using an out keyword must be initialized in the called method before it returns back to calling method.
Example using ref and out:
using System; class Program { static void Main() { string value1 = "cat"; // Assign string value SetString1(ref value1); // Pass as reference parameter Console.WriteLine(value1); // Write result string value2; // Unassigned string SetString2(1, out value2); // Pass as out parameter Console.WriteLine(value2); // Write result } static void SetString1(ref string value) { if (value == "cat") // Test parameter value { Console.WriteLine("Is cat"); } value = "dog"; // Assign parameter to new value } static void SetString2(int number, out string value) { if (number == 1) // Check int parameter { value = "one"; // Assign out parameter } else { value = "carrot"; // Assign out parameter } } }
Output:
Is cat
dog
one
Ref and out in method overloading
Both ref and out cannot be used in method overloading simultaneously. However, ref and out are treated differently at run-time but they are treated same at compile time (CLR doesn't differentiates between the two while it created IL for ref and out). Hence methods cannot be overloaded when one method takes a ref parameter and other method takes an out parameter. The following two methods are identical in terms of compilation.
class MyClass { public void Method(out int a) // compiler error “cannot define overloaded” { // method that differ only on ref and out" } public void Method(ref int a) { // method that differ only on ref and out" } }
However, method overloading can be done, if one method takes a ref or out argument and the other method takes simple argument. The following example is perfectly valid to be overloaded.
class MyClass { public void Method(int a) { } public void Method(out int a) { // method differ in signature. } }
Note:
The out and ref keywords are useful when we want to return a value in the same variables as are passed as an argument.
Ref Vs Out
Ref
|
Out
|
The parameter or
argument must be initialized first before it is passed to ref.
|
It is not compulsory
to initialize a parameter or argument before it is passed to an out.
|
It is not required
to assign or initialize the value of a parameter (which is passed by ref)
before returning to the calling method.
|
A called method is
required to assign or initialize a value of a parameter (which is passed to
an out) before returning to the calling method.
|
Passing a parameter
value by Ref is useful when the called method is also needed to modify the
pass parameter.
|
Declaring a
parameter to an out method is useful when multiple values need to be returned
from a function or method.
|
It is not compulsory
to initialize a parameter value before using it in a calling method.
|
A parameter value
must be initialized within the calling method before its use.
|
When we use REF,
data can be passed bi-directionally.
|
When we use OUT data
is passed only in a unidirectional way (from the called method to the caller
method).
|
Both ref and out are
treated differently at run time and they are treated the same at compile
time.
|
|
Properties are not
variables, therefore it cannot be passed as an out or ref parameter.
|
No comments:
Post a Comment