C# Tuple

C# Tuple is a data structure introduced in 4.0. It has elements with multiple parts i.e., more than one data type. In a Tuple, you can easily store duplicate elements. Also, a tuple returns values from a method without using the out parameter.

In C# Tuples, elements can only be added from 1 to 8. An error would be thrown, if you try to add more than 8 elements. However, you can use Nested Tuple to add more than 8 elements. To display the 8th element, use the Rest property. Rest, the 1st 7 items can be accessed using Item1, Item2, Item3, etc. properties.

Create a Tuple

To create a Tuple, there are two ways:

  • Create a Tuple using its Constructor
  • Create a Tuple using the Create() method

Create a Tuple using its constructor

Let us see an example of creating a tuple. We will also see how to access the values using the Item property. The 1st 7 items can be accessed using the Item property, i.e., Item1, Item2, Item3, etc. properties:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       // Create a 2-tuple
       Tuple<string, string>myTuple = new Tuple<string, string>("Sachin", "India");

       // Display the tuple items
       System.Console.WriteLine("Player {0} from {1}.", myTuple.Item1, myTuple.Item2);
     }
  }
}

Output

Player Sachin from India.

Create a Tuple using the Create() method

Let us see an example of creating a tuple using the Create() method. We will also see how to access the values using the Item property. The 1st 7 items can be accessed using the Item property, i.e., Item1, Item2, Item3, etc. properties:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       // Create a 2-tuple using the Create() method
       var myTuple = Tuple.Create("Sachin", "India");

       // Display the tuple items
       System.Console.WriteLine("Player {0} from {1}.", myTuple.Item1, myTuple.Item2);
     }
  }
}

Output

Player Sachin from India.

Access Tuple elements/ items

Since, a Tuple in C# can have 8 elements, to display the 8th element, use the Rest property. Rest, the 1st 7 items can be accessed using the Item property, i.e., Item1, Item2, Item3, etc. properties.

Let us see an example and create a Tuple with 8 elements and access each element one by one:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       // Create a 8-tuple using the Create() method
       var myTuple = Tuple.Create("India", "Australia", "England", "Bangladesh", "Sri Lanka", "New Zealand", "West Indies", "Afghanistan");

       // Display the tuple items
       System.Console.WriteLine("Country 1 = "+myTuple.Item1);
       System.Console.WriteLine("Country 2 = "+myTuple.Item2);
       System.Console.WriteLine("Country 3 = "+myTuple.Item3);
       System.Console.WriteLine("Country 4 = "+myTuple.Item4);
       System.Console.WriteLine("Country 5 = "+myTuple.Item5);
       System.Console.WriteLine("Country 6 = "+myTuple.Item6);
       System.Console.WriteLine("Country 7 = "+myTuple.Item7);
       
       // For the 8th item, use the Tuple Rest property
       System.Console.WriteLine("Country 8 = "+myTuple.Rest);
     }
  }
}

Output

Country 1 = India
Country 2 = Australia
Country 3 = England
Country 4 = Bangladesh
Country 5 = Sri Lanka
Country 6 = New Zealand
Country 7 = West Indies
Country 8 = (Afghanistan)

Nested Tuples

Use Nested Tuples in C# to add more than 8 elements. Those can be easily accessed using the Rest property in C#. Let us see an example:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Studyopedia
{
  class Program
  {
    static void Main(string[] args)
    {
       // Create a 8-tuple using the Create() method
       var myTuple = Tuple.Create("India", "Australia", "England", "Bangladesh", "Sri Lanka", "New Zealand", "West Indies", Tuple.Create(100, 200));

       // Display the tuple items
       System.Console.WriteLine("Country 1 = "+myTuple.Item1);
       System.Console.WriteLine("Country 2 = "+myTuple.Item2);
       System.Console.WriteLine("Country 3 = "+myTuple.Item3);
       System.Console.WriteLine("Country 4 = "+myTuple.Item4);
       System.Console.WriteLine("Country 5 = "+myTuple.Item5);
       System.Console.WriteLine("Country 6 = "+myTuple.Item6);
       System.Console.WriteLine("Country 7 = "+myTuple.Item7);
       
       // For the Nested Tuple items, use the Tuple Rest property
       System.Console.WriteLine("Nested Tuple Item 1 = "+myTuple.Rest.Item1.Item1);
       System.Console.WriteLine("Nested Tuple Item 2 = "+myTuple.Rest.Item1.Item2);
     }
  }
}

Output

Country 1 = India
Country 2 = Australia
Country 3 = England
Country 4 = Bangladesh
Country 5 = Sri Lanka
Country 6 = New Zealand
Country 7 = West Indies
Nested Tuple Item 1 = 100
Nested Tuple Item 2 = 200

To get only the nested tuple, use the following:

myTuple.Rest.Item1

The output would display the entire Nested Tuple:

(100, 200)

Read More

C# Dictionary
C# Stack
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment