Byron Sommardahl
Byron Sommardahl

Reputation: 13002

What's a valid reason to use an 'out' or 'ref' parameter in a method?

I despise out's and ref's as parameters on methods. IMHO, they make code less clean and provide opportunities for side-effects. But I concede that I may not understand their usefulness which might explain part of my loathing. Please, can someone explain a valid case for out's or ref's?

Upvotes: 17

Views: 1241

Answers (4)

xanatos
xanatos

Reputation: 111860

The "new" (C# 4.0) lock uses the ref (technically the lock statement is syntactic sugar for the new Monitor.Enter overload). It wouldn't be possible to do it without :-)

bool acquiredLock = false;

try
{
    Monitor.Enter(lockObject, ref acquiredLock);

    // Code that accesses resources that are protected by the lock.

}
finally
{
    if (acquiredLock)
    {
        Monitor.Exit(lockObject);
    }
}

so there is space for the ref in highly critical places.

The reason why simply returning a bool value wouldn't be enough is that an exception could occur between the return true of the Monitor.Enter and the bool acquiredLock = leaving your program with the question "was the lock tacken?" With the ref parameter this problem is solved.

Upvotes: 6

Jon Skeet
Jon Skeet

Reputation: 1500675

Basically if you need to return more than one value, it's an alternative to using something like Tuple<,> or a custom type to encapsulate the values. The canonical example is probably int.TryParse and related methods. They want to convey two pieces of information back:

  • The parsed value
  • Whether or not parsing succeeded.

Now these could actually have been written using a return type of int? etc in this case, but it's the same principle for other cases. (For example, Dictionary<,>.TryGetValue, where the value stored in the dictionary may legitimately be null.)

I wouldn't say I despise out and ref parameters, but I do believe they should only be used occasionally, and only when there isn't a better alternative. Most of the uses of ref I see on Stack Overflow are due to a misunderstanding of parameter passing.

Upvotes: 21

Mark Byers
Mark Byers

Reputation: 838276

out provides a way to return more than one value. ref is the same, except that you can also pass in a value.

Note that you can mutate an object even if is declared without ref (assuming it is mutable in the first place of course).

The alternative I prefer to out is to make a class to contain all the return values and return a single instance of that class.

Upvotes: 7

Glory Raj
Glory Raj

Reputation: 17701

They're pretty much the same - the only difference is that a variable you pass as an out parameter doesn't need to be initialised, and the method using the out parameter has to set it to something.

int x;
Foo(out x); 

int y;
Foo(ref y); 

Ref parameters are for data that might be modified, out parameters are for data that's an additional output for the function (eg int.TryParse) that are already using the return value for something.

Upvotes: 2

Related Questions