C# has the
ref keywords. Why are these two required?
Disclaimer: I don't have deep knowledge of C#.
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
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
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
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
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.
At their core both
out provide a mechanism for passing the address of a variable to a method so that it can be updated - assigning to the
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.