I prefer bool return using out methods for TryXXX such as TryParse , TryDeque , etc. for several reasons.
- The "parts" of the result are very different things, and I like that they are stored separately.
- The return type is the same in all variants of this. So it’s consistent.
- The
out type is always closely related to the type or object called either by the type itself (as in int.TryParse ) or by the type parameter for the type (as in Dictionary<TKey, TValue>.TryGet and ConcurrentQueue<T>.TryDequeue ). So it’s consistent. - It becomes an easily recognizable pattern inside .NET. So it’s consistent.
The last point has a downside in that people can turn to it when something is more appropriate (provided that it is entered correctly and successful, and exceptions are thrown, if it is not, it is often better).
In other cases, I avoid out , it does not catch well, which, in turn, is also not very convenient in lambda expressions.
I prefer an object that encapsulates different values when it is most likely to be used on its own. Otherwise, it's just another class you can learn about.
I prefer tuples when there is a good clear reason why something returns two different values at the same time, but this does not correspond to the above question about the new use-object itself.
First of all, I prefer to return only one result.
With an example of a car error, I would either return one object that would represent an error that could be zero if it weren’t faulty — the only object that represented the state of the car from which “no errors were detected” could be a possible value or, most importantly, because cars may have more than one error (and, apparently, are developing several expensive things immediately before the equivalent of your country's NCT / MOT /) of an enumerated or requested object that would allow me to sort through or request to find all faults that would be empty (as in Count == 0 ) if there were no errors.
Jon hanna
source share