One major use case I can think of off the top of my head are Try...
methods that return a value and a boolean so that you can check whether the action succeded or not:
// With out parameters:
if(int.TryParse(someString, out int result)){
// do something with the int
}
// With tuples:
var (success, value) = int.TryParseWithTuples(someString);
if(success){
// do something with the int
}
With the out
parameter, the notation is cleaner and results in less lines (and doesn't require you to create a local variable for the success boolean). It also allows you to do this:
if(int.TryParse(someString, out int r1)){
// do something with the int
} else if(int.TryParse(fallbackString, out int r2)){
// do something with the fallback int
} else {
throw new InvalidOperationException();
}
With tuples, this would look like this:
var (success, value) = int.TryParseWithTuples(someString);
if(success){
// do something with the int
} else {
(success, value) = int.TryParseWithTuples(fallbackString);
if(success){
// do something with the fallback int
} else {
throw new InvalidOperationException();
}
}
TryDoSomething
pattern (returns Boolean true on success, with the output in anout
parameter) relies on it. It's been around since day 1 (I thinkdouble.TryParse
was in v1.0 of the Framework) – Sloan