What Is Tuple in C#

Introduction

 
Tuples were available before C# 7.0, but they were inefficient and had no language support. Issues in tuples in previous versions were that the elements in a tuple could only be referenced as Item1, Item2 and so on. C# 7.0 introduces improved tuples feature that enables semantic names for the fields of a tuple using new, more efficient tuple types.
 
Tuple is a class in C#.
 
Let us dive into it.
  1. class Program   
  2.     {   
  3.         static void Main(string[] args)   
  4.         {   
  5.             var Result = Calculation(9, 3);   
  6.             Console.WriteLine("Addition " + Result.Item1);   
  7.             Console.WriteLine("Multiplication " + Result.Item2);   
  8.             Console.WriteLine("Subtraction " + Result.Item3);   
  9.             Console.WriteLine("Division " + Result.Item4);   
  10.             Console.ReadLine();   
  11.         }   
  12.         public static Tuple<int,int,int,int> Calculation(int x, int y)   
  13.         {   
  14.             int Add = x + y; // Addition    
  15.             int Mul = x * y; // Multiplication   
  16.             int Sub = x - y; // Subtraction     
  17.             int Div = x / y; // Division    
  18.             Tuple<int,int,int,int> t = Tuple.Create(Add, Mul, Sub, Div);   
  19.             return t;    
  20.         }   
  21.     }   
  22. }  

Slightly changed in C#

  1. class Program   
  2.     {   
  3.         static void Main(string[] args)   
  4.         {   
  5.             var Result = SumResult(9, 3);   
  6.             Console.WriteLine("Addition " + Result.Item1);   
  7.             Console.WriteLine("Multiplication " + Result.Item2);   
  8.             Console.WriteLine("Subtraction " + Result.Item3);   
  9.             Console.WriteLine("Division " + Result.Item4);   
  10.             Console.ReadLine();   
  11.         }   
  12.         public static (int,int,int,int) SumResult(int x, int y)   
  13.         {   
  14.             int Add = x + y; // Addition    
  15.             int Mul = x * y; // Multiplication   
  16.             int Sub = x - y; // Subtraction     
  17.             int Div = x / y; // Division    
  18.             return (Add, Mul, Sub,Div);   
  19.         }  
  20.     }  

Feature of Tuple

  1. class Program   
  2.     {   
  3.         static void Main(string[] args)   
  4.         {   
  5.             var (Addition, Multiplication, Subtraction, Division) = SumResult(9, 3);   
  6.             Console.WriteLine("Addition " + Addition);   
  7.             Console.WriteLine("Multiplication " + Multiplication);   
  8.             Console.WriteLine("Subtraction " + Subtraction);   
  9.             Console.WriteLine("Division " + Division);   
  10.             Console.ReadLine();   
  11.         }   
  12.         public static (int,int,int,int) SumResult(int x, int y)   
  13.         {   
  14.             int Add = x + y; // Addition    
  15.             int Mul = x * y; // Multiplication   
  16.             int Sub = x - y; // Subtraction     
  17.             int Div = x / y; // Division    
  18.             return (Add, Mul, Sub,Div);   
  19.         }   
  20.     }  

We can define in this way:

  1. static void Main(string[] args)   
  2. {   
  3.             var result = SumResult(9, 3);   
  4.             Console.WriteLine("Addition " + result.Addition);   
  5.             Console.WriteLine("Multiplication " + result.Multiplication);   
  6.             Console.WriteLine("Subtraction " + result.Subtraction);   
  7.             Console.WriteLine("Division " + result.Division);   
  8.             Console.ReadLine();   
  9. }   
  10. public static (int Addition, int Multiplication, int Subtraction, int Division) SumResult(int x, int y)   
  11. {   
  12.             int Add = x + y; // Addition    
  13.             int Mul = x * y; // Multiplication   
  14.             int Sub = x - y; // Subtraction    
  15.             int Div = x / y; // Division    
  16.             return (Add, Mul, Sub, Div);   
  17. }  

Conclusion

 
In this article, we studied about tuples in C#.


Similar Articles