Why use "out" instead of ref?

I wrote a few blocks of code about the refoutout declaration. I think ref is most useful. OK. why should i use. I can always use ref ref everytime:

namespace out_ref { class Program { static void Main(string[] args) { sinifA sinif = new sinifA(); int test = 100; sinif.MethodA(out test); Console.WriteLine(test.ToString()); sinif.MethodB(ref test); Console.WriteLine(test.ToString()); Console.ReadKey(); } } class sinifA { public void MethodA(out int a) { a = 200; } int _b; public void MethodB(ref int b) { _b = b; b = 2*b; } } } 
+4
source share
5 answers

Yes, you can use ref every time, but they have different goals. ref is used when the parameter is both input and output. out is used when the parameter is only an output. It can be used to pass input, but it makes it so that the user of the function does not need to declare an instance before using the function, because you are actually declaring that you guarantee the creation of the instance. This is especially useful in the TryXXX template when you get a value from a collection.

+13
source

When you have a parameter with out attached to it, you do not need to initialize it before passing it to the method that accepts it.

 The out keyword causes arguments to be passed by reference. This is like the ref keyword, except that ref requires that the variable be initialized before it is passed. 

From: http://msdn.microsoft.com/en-us/library/ee332485.aspx

+6
source

The out parameter must be initialized during the function. This provides a more stringent contract with your subscriber. It also allows you to write code as follows:

 int i; MyFunc(out i); .. < use i > .. 

without getting a compiler error for an uninitialized variable.

+3
source

ref and out treated the same inside, they pass the variable by reference.

The difference lies in the semantics of initialization, which is responsible for the fact that the variable has a valid initial value and is executed by the compiler.

With out parameters, this is the method you are calling. Any value in a variable that you pass by reference before you call a method does not matter. The compiler will complain if the method you call has an execution path that cannot set a new value for its parameter. This means that any value in the variable before you call the method is guaranteed to be overwritten, even if it has the same value.

With ref parameters, this is the method that calls. The value of the variable will be passed to the called method, which can then check its parameter and possibly change it. The compiler will complain about a method that calls does not initialize the variable with the value before making the call.

So basically it all depends on what the compiler will complain about. This is the calling code that should ensure that the variable has a value or that it is the called code.

+1
source

Output options

In addition to passing values ​​by reference, you can also indicate that this parameter is out using the out keyword, which is used in the same way as the ref keyword (as a modifier to the parameter in the function definition and function call). In essence, this gives you almost exactly the same behavior as the reference parameter, in that the parameter value at the end of the function execution is returned to the variable used in the function call. However, there are important differences:

  • While it is illegal to use an unrecognized variable as the ref parameter, you can use an unrecognized variable as the out parameter
  • The out parameter should be considered as an unassigned value by the function that uses it.

This means that although the assigned variable is used as the out parameter in the code call, the value stored in this variable is lost when the function is executed.

As an example, consider the extension of the MaxValue () function shown earlier, which returns the maximum value of an array. Change the function a bit to get the index of the element with the maximum value inside the array. To keep things simple, get only the index of the first occurrence of this value in the presence of several elements with a maximum value. To do this, add the out parameter by changing the function as follows:

 static int MaxValue(int[] intArray, out int maxIndex) 

{int maxVal = intArray [0]; maxIndex = 0; for (int i = 1; i <intArray.Length; i ++) {if (intArray [i]> maxVal) {maxVal = intArray [i]; maxIndex = i; }} return maxVal; }

You can use this function as shown below:

 int[] myArray = {1, 8, 3, 6, 2, 5, 9, 3, 0, 2}; 

int maxIndex; Console.WriteLine ("The maximum value in myArray is {0}", MaxValue (myArray, out maxIndex)); Console.WriteLine ("The first occurrence of this value is in the element {0}", maxIndex + 1);

The result is the following:

The maximum value in myArray is 9

The first occurrence of this value is in element 7

One is added to the maxIndex value returned here when it is displayed on the screen. This is to translate the index into a more readable form, so the first element in the array is called element 1, not element 0.

0
source

Source: https://habr.com/ru/post/1311313/


All Articles