Problem Statement: Want to return multiple results from the single method of different types.

To achieve this functionality, there are three methodologies until Framework version 4.6.* and following are those

  • Method-1: Using out parameters: As a .NET developer working since framework 3.5 will aware of out parameter feature where 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 storing more data will be heap memory overhead and GC process time to clearing the objects.
      • When we output of type Tuple, 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 of creating multiple POCO classes and managing them and also this will be a reference type and again same heap memory overhead.

By keeping all these points into consideration, the team came with a solution called Value Tuple data structure in 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 won’t occupy heap memory instead works with stack memory.

Following are the declaration

public struct ValueTuple : IEquatable<ValueTuple>, IStructuralEquatable,  
IStructuralComparable, IComparable, IComparable<ValueTuple>

Let’s dig to know how to work with ValueTuples.

System.ValueTuple won’t come with framework 4.7 package instead we need to install it from the NuGet package as shown below.

We can declare this type in two ways and following are those

Declaration – 1: Here this declaration of the method we are trying to return student name and address.

public (string, string) GetStudentNameAddress(int studentId) 
        { 
            return ("Sai", "Hyderabad"); 
        }

As you can see above method which demonstrates how to return and declare a method using valueType. For the same below is the statement where we are trying to assign this return values to an object.

 

Var result =  GetStudentNameAddress(10);
Console.writeline($"{result.Item1} and {result.Item2}");

As you can observe in above statement still we are not comfortable with Item1 and Item2. To overcome this we have a feature in ValueType where it supports named properties which are demonstrated in Declaration 2.

Declaration-2: As explained above this declaration will give more comfort with custom named parameters.

public (string Name, string Address) GetStudentNameAddress(int studentId) 
        { 
            return ("Sai", "Hyderabad"); 
        }

As observed we can give a type and name of the parameter the same will be passed to result in variable as follows

Var result =  GetStudentNameAddress(10); 
Console.writeline($"{result.Name} and {result.Address}");

Huh, now we solved the issue of Item1 and 2 with our custom names.

For more comfort, we can even have following styles of usage and which are self-explanatory.

Style-1:  

Var result =  GetStudentNameAddress(10);

Style-2: 

 (string Name, string Address) =  GetStudentNameAddress(10);

Style-3: 

String (Name, Address) =  GetStudentNameAddress(10);

Hope you understand well, please comment here if any.

Happy coding 🙂