This is among one of the most important questions asked in C# interviews. It is very well known that both ref and out parameters are used to pass an argument within a method.
But the question here is, what are the scenarios we need them and which one should we prefer.
We always heard that in C# there are two types of “types”:
- Value types contain the data itself.
- Reference types have their value as a reference to the data, instead of the data itself.
We are pretty much aware of the value type. So the main concern here is for reference type. In case of the reference type we can use either out or ref keyword.
Reference parameters actually never pass the value of a variable in the invoked method, instead they use the variable themselves.
From the point of storage, instead of allocating a new storage for that variable in the method declaration, the same storage space is used, so in this case the value of the variable in the member method and the value of the reference parameter will always remain the same.
Let's take now the output parameter. At the first glance they seems to be much like each other.
Output parameters specified at the time of calling doesn't need to have been assigned a value before it is passed to the invoked method. When the method is invoked completely we can read that variable as it is assigned by now.
Following the same pattern as reference parameters, output parameters also do not create a new storage location, but use the storage location of the variable specified on the invocation. Output parameters need the out modifier as part of both the declaration and the invocation. That means it's always clear when you're passing something as an output parameter.
Note: Reference parameters require ref modifier and Output parameters need the out modifier as part of both the invoking and invoked method.
- int x;
- Foo(out x);
-
- int x;
- Foo(ref x);
Which one to choose
ref parameters are for data that might be modified, out parameters are for data that's an additional output for the function (for example int.TryParse) that are already using the return value for something.
We should use out unless we need ref.
It makes a big difference when the data needs to be marshalled, for example to another process that can be costly. So you want to avoid marshalling the initial value when the method doesn't make use of it.
Beyond that, it also shows the reader of the declaration or the call whether the initial value is relevant (and potentially preserved), or thrown away.
As a minor difference, an out parameter need not be initialized.
The following is an example of out:
- string a, b;
- person.GetBothNames(out a, out b);
Where GetBothNames is a method to retrieve two values atomically, the method won't change the behaviour of whatever a and b are. If the call goes to a server in Hawaii, copying the initial values from here to Hawaii is a waste of bandwidth.
The following is a similar snippet using ref:
- string a = String.Empty, b = String.Empty;
-
- person.GetBothNames(ref a, ref b);
That however could confuse readers, because it looks like the initial values of a and b are relevant (though the method name would indicate they are not).
The following is an example for ref:
- string name = textbox.Text;
-
- bool didModify = validator.SuggestValidName(ref name);
Here the initial value is relevant to the method.
From the compiler's point of view
ref tells the compiler that the object is initialized before entering the function, whereas out tells the compiler that the object will be initialized inside the function.
So whereas ref is two-ways, out is out-only.
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 the same at compile time (the CLR doesn't differentiate between the two when it creates IL for ref and out).
Hence methods cannot be overloaded when one method takes a ref parameter and the other method takes an out parameter. The following two methods are identical in terms of compilation.
- class Example1 {
-
-
- public void SampleMethod(out int i) {}
- public void SampleMethod(ref int i) {}
- }
However, method overloading can be done, if one method takes a ref or out argument and the other method takes a simple argument. The following example is perfectly valid to be overloaded.
- class Example2
- {
- public void SampleMethod(int i) { }
- public void SampleMethod(ref int i) { }
- }
Or:
- class Example3
- {
- public void SampleMethod(int i) { }
- public void SampleMethod(out int i) { }
- }
Like all out parameters, an out parameter of an array type must be assigned before it is used; that is, it must be assigned by the callee.
- static void TestMethod1(out int[] arr)
- {
- arr = new int[10];
- }
Like all ref parameters, a ref parameter of an array type must be definitely assigned by the caller. Therefore, there is no need to be definitely assigned by the callee. A ref parameter of an array type may be altered as a result of the call.
For example, the array can be assigned the null value or can be initialized to a different array.
- static void TestMethod2(ref int[] arr)
- {
- arr = new int[10];
- }
Example: The array theArray is declared in the caller (the Main method), and initialized in the FillArray method. Then, the array elements are returned to the caller and displayed.
- class TestOut {
- static void FillArray(out int[] arr) {
-
- arr = new int[5] {
- 1, 2, 3, 4, 5
- };
- }
-
- static void Main() {
- int[] theArray;
-
-
- FillArray(out theArray);
-
-
- System.Console.WriteLine("Array elements are:");
- for (int i = 0; i < theArray.Length; i++) {
- System.Console.Write(theArray[i] + " ");
- }
-
-
- System.Console.WriteLine("Press any key to exit.");
- System.Console.ReadKey();
- }
- }
Output
The array elements are:
1 2 3 4 5
In this example, the array theArray is initialized in the caller (the Main method), and passed to the FillArray method using the ref parameter. Some of the array elements are updated in the FillArray method. Then, the array elements are returned to the caller and displayed.
- class TestRef {
- static void FillArray(ref int[] arr) {
-
- if (arr == null) {
- arr = new int[10];
- }
-
- arr[0] = 1111;
- arr[4] = 5555;
- }
-
- static void Main() {
-
- int[] theArray = {
- 1, 2, 3, 4, 5
- };
-
-
- FillArray(ref theArray);
-
-
- System.Console.WriteLine("Array elements are:");
- for (int i = 0; i < theArray.Length; i++) {
- System.Console.Write(theArray[i] + " ");
- }
-
-
- System.Console.WriteLine("Press any key to exit.");
- System.Console.ReadKey();
- }
- }
Output
Array elements are:
1111 2 3 4 5555
The following are some points to remember:
-
Properties cannot be passed via out or ref, since properties are actually methods.
-
There is no "boxing" when a value type is passed by reference.
-
ref / out are not considered to be a part of the method signature at compile time, so methods cannot be overloaded, if the only difference between them is that one of the methods takes a ref argument and the other takes an out argument.
Thanks for visiting my blog.
Thanks for reading my article.
This is my first article here. Please provide your comments if you need any clarifications. It will be educational for me as well.