18

C# has the out and ref keywords. Why are these two required? Disclaimer: I don't have deep knowledge of C#.

4
  • 3
    Isn't there a cross-site duplicate on Stack Overflow? Jul 26, 2015 at 20:19
  • 6
  • 2
    Are you asking for the difference between ref and out? Or are you asking what the usage of by-reference arguments ref/out is as compared to by-value arguments (no modifier to the parameter)? Or both? Jul 27, 2015 at 7:39
  • 1
    I feel this question is similar, but not quite a dup -- at least if it is taken from language design perspective
    – jmoreno
    Jul 31, 2015 at 3:41

4 Answers 4

33

Implementation-wise, they're essentially the same thing, but out communicates something that ref doesn't: you don't care about input. This means certain things to you and to tooling. For example, you can pass an uninitialized variable to an out parameter and that's not a problem, where otherwise it would be, and within the function you are required to assign a value to the out parameter before it returns, whereas there's no requirement to do anything at all with a ref value.

5
  • 12
    Ref vs out is far more important for the compiler than it is to a programmer. The compiler is obliged to make certain you do not read a variable before it is assigned a value, and out gives it a way to do so which better matches how you want to deal with the variables in the first place.
    – Cort Ammon
    Jul 26, 2015 at 18:38
  • Could you please elaborate about : 'out' communicates something that 'ref' doesn't.
    – Neo
    Jul 26, 2015 at 19:21
  • 4
    @Neo: Basically what I said in the answer: It communicates that you don't care about using this for input, and thus it's safe to pass an uninitialized value to an out param because the function receiving it isn't going to read from it. Jul 26, 2015 at 19:30
  • @MasonWheeler Is this allowed (and/or enforced on the receiving end) by the verifier, though? Out is just an attribute, internally.
    – Random832
    Jul 26, 2015 at 19:33
  • @Random832: As Robert posted under his answer, have a look at Eric Lippert (former C# compiler guy) discussing this at stackoverflow.com/q/2876315 Jul 26, 2015 at 19:37
8

To understand these two keywords, you'll need to understand what passing a value by reference means. Essentially it means that, if you modify the parameter variable that you pass in within the method or function, that variable retains its modified value when your function exits. Declaring a parameter as ref or out accomplishes this, whereas an ordinary parameter variable would retain its original value, even if you modified it in the function.

The only differences between ref and out is that out doesn't require you to initialize the variable before you pass it into the function, but ref does; while out does require you to assign something to the variable within the function, but ref doesn't.

2
2

You asked.

C# have out and ref, why these two are required. I don't have depth knowledge of C#. Why out and ref parameters are required?

And the answer is that they aren't REQUIRED, java for instance doesn't have either and VB only has ref (and only requires it in the method declaration).

You can easily write an entire program without using either.

They exist because they are occasionally useful. Which is why there are two of them and not just one -- because it is useful to make the distinction between 'here is something, modify it if you want' and 'give this thing a new value'.

Other languages have different priorities and so may or may not include these or equivalent. They may even disagree on whether either is useful, and so may actively decide NOT to implement one or the other.

1

At their core both ref and out provide a mechanism for passing the address of a variable to a method so that it can be updated - assigning to the ref or out parameter updates the variable in the caller's context. Where they differ is in the guarantees: out guarantees that the variable will be updated, ref does not.

From the other side of the call there are similar differences. An out parameter to our method is not guaranteed to contain anything useful on arrival and must be set to a value at some point, while a ref parameter can be assumed to have a value and we don't need to do anything specific with it.

This difference has impacts on code analysis, generation and optimization which, while not terribly apparent to us most of the time, can have some effects on the way our programs end up operating. The code generator can defer allocation of the variable until immediately prior to the call, the optimizer might decide it can reorder some operations more effectively based on the contract, etc.

In terms of usage: as a general rule I use out for situations where I don't care about the content of the variable when it arrives as a parameter and ref when I do.

Not the answer you're looking for? Browse other questions tagged or ask your own question.