Is there a benefit to using the c# out/ref keyword to "return" > 1 item in a method, instead of an object

StackOverflow https://stackoverflow.com/questions/18477557

  •  26-06-2022
  •  | 
  •  

Question

Returning multiple things from a method, involves either:

  • returning an object with properties OR
  • using the out keyword to simply modify incoming parameters

Is there a benefit to using one system or the other? I have been using objects, but just discovered the out keyword, so wondering if I should bother refactoring.

Was it helpful?

Solution 2

In general, if the object that you are returning is not used anywhere else outside of the return value of your method or a group of similar methods, it is a good indication that you should refactor. When you need to create a special class simply to be used as a return value of a method, it means that you are working around C#'s inability to return multiple values from a method, so the out keyword may be a very good option for you.

On the other hand, if you use the multi-part return value in other places, such as storing them in collections or passing as arguments to other methods, there's probably no need to refactor, because the return object is meaningful.

Compare these two methods:

interface DictionaryReturn<T> {
    T Value {get;}
    bool Success {get;}
}
...
class Dictionary<K,V> {
    ...
    public DictionaryReturn<V> TryGetValue(K key) {
        ...
    }
}

or

class Dictionary<K,V> {
    ...
    public bool TryGetValue(K key, out V res) {
        ...
    }
}

The first case introduces a special DictionaryReturn<T> class that provides the value and an indicator that the value was found in the dictionary. There is rarely, if ever, a reason to store or use DictionaryReturn<T> objects outside the call to TryGetValue, so the second option is better. Not surprisingly, it is the second option that the designers of the .NET collections library have implemented.

OTHER TIPS

You shouldn't bother refactoring just to utilize out parameters. Returning a class or struct would be preferred as long as structure is reusable.

A common use for out parameters which I would suggest using is to return a status for a call with that is possible to fail. An example being int.TryParse.

It has the possibility of failing, so returning a bool makes it easy to determing whether or not you should use the out parameter.


Another possible solution to returning multiple values from a method would be to use a Tuple. They can return n number of results. E.g.

public Tuple<bool, bool, string> MyMethod()
{
    return new Tuple<bool, bool, string>(false, true, "yep");
}

I prefer to use Object with properties. If you use out keyword, you need to define it in other line. It is not as clear as return Object;

The reason to use out keyword is to ensure that code inside the method always sets a value to the out parameter. It's a compile time check that what you intended to do in the function, you did do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top