C# Tuples

C# tuple is a data structure that is used to store sequence of elements. Tuple with n elements are known as n-tuple.

We can use Tuple for the following reasons.

  • To represent a single set of data
  • To provide easy access and manipulation of data
  • To return multiple values from a method without using out parameter
  • To pass multiple values to a method through a single parameter

In C#, tuple is a class that contains static methods. These methods are used to create tuple objects. Following is the syntax of Tuple class.

C# Tuple Class Syntax

  1. public static class Tuple  

C# Tuple Methods

The following table contains methods of Tuple class.

Method nameDescription
Create<T1>(T1)It is used to create a new 1-tuple, or singleton.
Create<T1,T2>(T1,T2)It is used to create a new 2-tuple, or pair.
Create<T1,T2,T3>(T1,T2,T3)It is used to create a new 3-tuple, or triple.
Create(T1,T2,T3,T4)It is used to create a new 4-tuple, or quadruple.
Create<T1,T2,T3,T4,T5>(T1,T2,T3,T4,T5)It is used to create a new 5-tuple, or quintuple.
Create<T1,T2,T3,T4,T5,T6>(T1,T2,T3,T4,T5,T6)It is used to create a new 6-tuple, or sextuple.
Create<T1,T2,T3,T4,T5,T6,T7>(T1,T2,T3,T4,T5,T6,T7)It is used to create a new 7-tuple, or septuple.
Create<T1,T2,T3,T4,T5,T6,T7,T8>(T1,T2,T3,T4,T5,T6,T7,T8)It is used to create a new 8-tuple, or octuple.

Let's see an example that creates a Tuple to store book information. Here, we are creating Tuple without using static method.

C# Tuple Example 1

  1. using System;  
  2. namespace CSharpFeatures  
  3. {  
  4.     class TupleExample  
  5.     {  
  6.         public static void Main(string[] args)  
  7.         {  
  8.             // Creating Tuple of three values  
  9.             var book = new Tuple<stringstringdouble>("C# in Depth""Jon Skeet", 100.50);  
  10.             Console.WriteLine("-----------------Book's Record---------------------");  
  11.             Console.WriteLine("Title  "  + book.Item1);  
  12.             Console.WriteLine("Author " + book.Item2);  
  13.             Console.WriteLine("Price  "  + book.Item3);  
  14.         }  
  15.     }  
  16. }  

Output:

-----------------Book's Record---------------------
Title  C# in Depth
Author Jon Skeet
Price  100.5

We can use static methods of Tuple class to create Tuple. In the following example, we are using create method.

C# Tuple Example with Static Method

  1. using System;  
  2. namespace CSharpFeatures  
  3. {  
  4.     class TupleExample  
  5.     {  
  6.         public static void Main(string[] args)  
  7.         {  
  8.             // Creating Tuple using helper method (Create)  
  9.             var book = Tuple.Create("C# in Depth""Jon Skeet", 100.50);  
  10.             Console.WriteLine("-----------------Book's Record---------------------");  
  11.             Console.WriteLine("Title  " + book.Item1);  
  12.             Console.WriteLine("Author " + book.Item2);  
  13.             Console.WriteLine("Price  " + book.Item3);  
  14.         }  
  15.     }  
  16. }  

Output:

-----------------Book's Record---------------------
Title  C# in Depth
Author Jon Skeet
Price  100.5

We can use tuple to return multiple values from a method. In the following example, we are returning a tuple to the caller method.

C# Tuple Example Return Multiple Values

  1. using System;  
  2. namespace CSharpFeatures  
  3. {  
  4.     public class Student  
  5.     {  
  6.         public static void Main(string[] args)  
  7.         {  
  8.             var (name,email) = Show();  
  9.             Console.WriteLine(name+" "+email);  
  10.         }  
  11.         static (string name, string email) Show()  
  12.         {  
  13.             return ("irfan","irfan@gmail.com");  
  14.         }  
  15.     }  
  16. }  

Comments

Popular posts from this blog

C# | Association, Aggregation and Composition

Throw vs Throw ex in C#

C# Extension Method