C # Passing reference type directly compared to out parameter

I have two methods:

public void A(List<int> nums) { nums.Add(10); } public void B(out List<int> nums) { nums.Add(10); } 

What is the difference between these two calls?

 List<int> numsA = new List<int>(); A(numsA); List<int> numsB = new List<int>(); B(out numsB); 

In general, I am trying to understand the difference between passing reference types as-is or as out.

+9
c #
source share
4 answers

In your example, method B cannot be compiled because the out parameter is considered uninitialized, so you must initialize it before you can use it. In addition, when calling a method with the out parameter, you must specify the out keyword on the call site:

 B(out numsB); 

And you do not need to initialize the variable numbsB before the call, because it will be overwritten by the method.

John Skeet has a great article explaining various ways of passing parameters: Passing parameters in C #

+7
source share

The non-ref, non-out parameter, as a local variable, indicates the storage location. If the storage location type is a reference type, then the storage location contains a reference to an instance of this type.

Feedback parameters and vice versa, on the contrary, contain a link to the storage location. This storage location can be a local variable, field, or array element. In other words, the parameters ref and out introduce another layer of indirection. If you have a reference type parameter or an output in a method, it thus represents a link to an object reference.

Why do you need a link to an object link? If you need to change the link to the object (as opposed to changing the object itself).

This is a useful technique in some narrow environments. For example, you can write a function that orders two queues, depending on which it has a lower value at the top:

 void OrderQueues(ref Queue<int> a, ref Queue<int> b) { if (a.Peek <= b.Peek) return; var temp = a; a = b; b = temp; } 
Options

Out are useful if you want to return more than one value from a method:

 void OldestAndYoungest(IEnumerable<Person> people, out Person youngest, out Person oldest) { youngest = null; oldest = null; foreach (var person in people) { if (youngest == null || person.Age < youngest.Age) youngest = person; if (oldest == null || oldest.Age < person.Age) oldest = person; } } 

In my experience, the parameters ref and out are quite rare and even less common with reference types.

Note that the ref parameter must be initialized by the caller, and the out parameter must be initialized by the called user. If you never assign a value to the ref parameter, this should probably be a β€œnormal” parameter. If you never assign a value to the out parameter, as in your example, your code will not compile.

+3
source share

The out keyword causes arguments to be passed by reference. This is similar to the ref keyword, except that ref requires the variable to be initialized before passing. To use the out parameter, both the method definition and the invocation method must explicitly use the keyword. For example:

http://msdn.microsoft.com/en-us/library/t3c3bfhx(v=vs.80).aspx

0
source share

In version B, a function has direct access to a variable. This is similar to the 'ref' keyword, except that the variable must be assigned from a function that takes a parameter. It allows you to return multiple values ​​from a function. And the call syntax is "B (out numsB);"

0
source share

All Articles