Working With Tuple In C# 7.0

In this article, let us see how to use the latest feature of Tuple in C# 7.0.

Usually when we want to return multiple values from a method, then we will create a model class with the required property and will return it as a return type from the method.

Sample code without Tuple,

  1. //Class with property required to return from method.  
  2. public class ReturnTypeWithoutTuple  
  3. {  
  4.         public int _int { get; set; }  
  5.         public List<long> _longlist { get; set; }  
  6. }  
  7. // Class with method return the ReturnTypeWithoutTuple  
  8. public class WithoutTupleApplication  
  9. {  
  10.  public ReturnTypeWithoutTuple WithoutTuple()  
  11.         {  
  12.   
  13.             ReturnTypeWithoutTuple returnTypeWithoutTuple = new ReturnTypeWithoutTuple();  
  14.             returnTypeWithoutTuple._int = 10;  
  15.             returnTypeWithoutTuple._longlist.Add(1);  
  16.             returnTypeWithoutTuple._longlist.Add(2);  
  17.   
  18.             return returnTypeWithoutTuple;  
  19.         }  
  20. }  
  21. static void Main(string[] args)    
  22. {    
  23.             WithoutTupleApplication withoutTupleApp = new WithoutTupleApplication();    
  24. //invoking the WithoutTuple method and getting the data here.  
  25.             ReturnTypeWithoutTuple _data = withoutTupleApp.WithoutTuple();    
  26.             //perform required logic with _data  
  27. }    

But with the latest feature in C# 7, we don’t need to create separate model class anymore for every return type. Instead, let us see how we can use the tuple feature to do the same functionality of the above code.

To use the tuple in the application, first, we have to add the System.ValueTuple from NuGet package.

Sample Code with Tuple along with custom names as a return type,

  1. public class WithTupleApplication  
  2. {  
  3.         public (int _int, List<long> _longlist) WithTuple()  
  4.         {  
  5.             int intvalue = 10;  
  6.             List<long> longlist = new List<long>();  
  7.             longlist.Add(1);  
  8.             longlist.Add(2);  
  9.   
  10.             return (intvalue, longlist);  
  11.         }  
  12. }  

The names for return type can be anything as you required.

  1. static void Main(string[] args)    
  2. {    
  3.            WithTupleApplication withTupleApplication = new WithTupleApplication ();    
  4.             var _data = withTupleApplication.WithTuple();    
  5.             //perform your logic here  _data._int and _data._longlist  
  6. }  

Now, we can access the returned values with the custom name. We can also return the tuple without the custom name as well like the below code snippet.

Sample Code with Tuple without custom names as return type,

  1. public class WithTupleApplication  
  2. {  
  3. public (int, List<long>) WithTuple()  
  4.         {  
  5.             int intvalue = 10;  
  6.             List<long> longlist = new List<long>();  
  7.             longlist.Add(1);  
  8.             longlist.Add(2);  
  9.   
  10.             return (intvalue, longlist);  
  11.         }  
  12. }  

The return type will be only the datatype. Now, let us call the method from our main method.

  1. static void Main(string[] args)    
  2. {    
  3.            WithTupleApplication withTupleApplication = new WithTupleApplication ();    
  4.             (int, List<long>)  _data = withTupleApplication.WithTuple();     
  5.             //perform your logic here  _data.Item1 and _data.Item2  
  6. }   

Advantage of tuple

Avoids creation of model required only for few places in the application.

Conclusion

Tuple is a very effective feature in C# 7.0 and it can be used when we want to return more than one value from a method. Tuple can return 8 values and if we want to return more values then we can use the nested tuple on the 8th place.


Similar Articles