Problem Statement
You want to return multiple results from a single method of different types.
To achieve this functionality, there were three methodologies until Framework version 4.6.* and they are the following.
Method 1- Using out parametersAs a .NET developer working since framework 3.5, I'm well aware of the out parameter feature using which, we can send these as parameters to the method where it should be assigned with appropriate data.
Disadvantage
Out and ref parameters are not supported in Async methods.
Method 2 - System.Tuple Type
This is a new type introduced in Framework 4.0, where it has the scope to return up to 8 component values from a single method.
Disadvantages
As this Tuple type is a reference type and stored, more data will be heaped onto memory overhead and GC process time to clear the objects.
When we get the output of Tuple types, parameter names will be by default as Item1, Item2, etc.. There is no scope to give custom names for this.
Method 3 - POCO Class
This is a traditional way to capture multiple results in a single return object. Using POCO class we can declare all result variables as properties and can return from the given method.
Disadvantage
There will be some overhead creating multiple POCO classes and managing them, and also this will be a reference type and again the same heap memory overhead.
By keeping all these points in consideration, the team came up with a solution called Value Tuple data structure in the latest C# 7.0 and .NET framework 4.7.
How will this ValueTuple overcome above problems?
Firstly, System.ValueTuple is a structure (struct type) so as we know struct can be treated, as value type will not occupy heap memory and instead works with stack memory.
Following are the declarations,
- public struct ValueTuple : IEquatable<ValueTuple>, IStructuralEquatable, IStructuralComparable, IComparable, IComparable<ValueTuple>
Let’s dig in to know how to work with ValueTuples.
System.ValueTuple won’t come with the framework 4.7 package, instead we need to install it from the NuGet package as shown below.


Shrimant TelgavePosted Sep 7, 2018, 8:38 AM
Nice article...............