A coworker and i were debating today whether or not it is ever useful to make this sort of a function:
private void MyFunction(ref MyClass variable)
{
}
The only advantage i can see is that it would allow you to set the original pointer to the variable to null… outside of that there would be no difference if you omitted the ref, correct?
Can you think of any reason to ever pass a ref string to a function?
Personally I’d prefer to return a
Tuplewith multiple values, but the basic point is to give multiple results to the caller. In the case of a ref parameter, the extra result will potentially replace the value in an existing variable. Note that this is very different to just working on the parameter itself. For example:is completely different to:
Whether the parameter type is a reference type or a value type is somewhat irrelevant – particularly when you consider immutable types such as
string, where there’s no equivalent to the latter approach above.