1

When my method needs to return more than one value, I can use out parameters or tuples.

Is this a personal preference or do we have a recommended practice documentation how to choose between the two options?

Prefer not to use a complex class type for every o such scenario, because my solution will be bloated with so many classes.

I understand that TryParse is an example exists in .net framework, however it was introduced before tuples.

  • Firstly you cant use out parameters on async method so in some situations they are not an option. Secondly, Value Tuples are structs and will *likely* live on the stack so they can be a light and good option for more complex results (depending on the situation), they also have special syntax which allows for succinct use. Lastly, what you use is basically whats good for you, and entirely dependent on what is consistent with your code base and individual needs, and mostly subjective – TheGeneral Sep 20 '20 at 23:48
  • @MichaelRandall, this is such a good point. Thank you! – maal0606 Sciene Sep 21 '20 at 04:22

1 Answers1

1

I think that if you have many results that you want to return, ValueTuple ( for .NET Framesowrk 4.7 and above ) may be better for you because is a struct and immutable, especially if you have many results to return, or you use some kind of API like LINQ and you dont want to create objects of all the result.

You can read also :

Returning two values, Tuple vs 'out' vs 'struct'

What's the difference between System.ValueTuple and System.Tuple?

AnGG
  • 679
  • 3
  • 9