Tuple Class in C#

The Tuple class in C# represents a data structure that can hold a sequence of elements. The Tuple<T1,T2,T3,T4,T5,T6,T7> class specifically represents a 7-tuple, also called a septet. Tuples provide a convenient way to group multiple values together without creating a custom class.

Tuples are commonly used for −

  • Easier access to a data set.
  • Easier manipulation of a data set.
  • To represent a single set of data.
  • To return multiple values from a method
  • To pass multiple values to a method

Syntax

Following is the syntax for creating a 7-tuple −

Tuple<T1,T2,T3,T4,T5,T6,T7> tuple = new Tuple<T1,T2,T3,T4,T5,T6,T7>(item1, item2, item3, item4, item5, item6, item7);

You can also use the static Create method −

var tuple = Tuple.Create(item1, item2, item3, item4, item5, item6, item7);

Properties of 7-Tuple

The 7-tuple has seven properties to access its elements −

  • Item1 − Get the value of the current Tuple's first component.

  • Item2 − Get the value of the current Tuple's second component.

  • Item3 − Get the value of the current Tuple's third component.

  • Item4 − Get the value of the current Tuple's fourth component.

  • Item5 − Get the value of the current Tuple's fifth component.

  • Item6 − Get the value of the current Tuple's sixth component.

  • Item7 − Get the value of the current Tuple's seventh component.

7-Tuple Structure Tuple<T1, T2, T3, T4, T5, T6, T7> Seven elements of different types Item1 Item2 Item3 Item4 Item5 Item6 Item7 Each item can be of a different type (T1, T2, T3...)

Using Constructor to Create 7-Tuple

Example

using System;

public class Demo {
    public static void Main(string[] args) {
        Tuple<int,int,int,int,int,int,int> tuple = new Tuple<int,int,int,int,int,int,int>(100, 150, 200, 300, 600, 1000, 2000);
        Console.WriteLine("Value (Item1)= " + tuple.Item1);
        Console.WriteLine("Value (Item2)= " + tuple.Item2);
        Console.WriteLine("Value (Item3)= " + tuple.Item3);
        Console.WriteLine("Value (Item4)= " + tuple.Item4);
        Console.WriteLine("Value (Item5)= " + tuple.Item5);
        Console.WriteLine("Value (Item6)= " + tuple.Item6);
        Console.WriteLine("Value (Item7)= " + tuple.Item7);
        
        if (tuple.Item5 == 600) {
            Console.WriteLine("Exists: Tuple Item 5 = " + tuple.Item5);
        }
        if (tuple.Item6 == 900) {
            Console.WriteLine("Exists: Tuple Item 6 = " + tuple.Item6);
        }
        if (tuple.Item7 == 2000) {
            Console.WriteLine("Exists: Tuple Item 7 = " + tuple.Item7);
        }
    }
}

The output of the above code is −

Value (Item1)= 100
Value (Item2)= 150
Value (Item3)= 200
Value (Item4)= 300
Value (Item5)= 600
Value (Item6)= 1000
Value (Item7)= 2000
Exists: Tuple Item 5 = 600
Exists: Tuple Item 7 = 2000

Using Create Method for Mixed Data Types

Example

using System;

public class Demo {
    public static void Main(string[] args) {
        var tuple = Tuple.Create("John", 25, true, 'A', 85.5, "Developer", 5);
        Console.WriteLine("Name: " + tuple.Item1);
        Console.WriteLine("Age: " + tuple.Item2);
        Console.WriteLine("Is Married: " + tuple.Item3);
        Console.WriteLine("Grade: " + tuple.Item4);
        Console.WriteLine("Score: " + tuple.Item5);
        Console.WriteLine("Job: " + tuple.Item6);
        Console.WriteLine("Experience: " + tuple.Item7 + " years");
        
        // Demonstrating type safety
        Console.WriteLine("Data types: " + tuple.Item1.GetType() + ", " + tuple.Item2.GetType());
    }
}

The output of the above code is −

Name: John
Age: 25
Is Married: True
Grade: A
Score: 85.5
Job: Developer
Experience: 5 years
Data types: System.String, System.Int32

Comparing Tuple Items

Example

using System;

public class Demo {
    public static void Main(string[] args) {
        Tuple<int,int,int,int,int,int,int> tuple = new Tuple<int,int,int,int,int,int,int>(100, 150, 200, 300, 600, 1000, 1000);
        Console.WriteLine("Value (Item1)= " + tuple.Item1);
        Console.WriteLine("Value (Item2)= " + tuple.Item2);
        Console.WriteLine("Value (Item3)= " + tuple.Item3);
        Console.WriteLine("Value (Item4)= " + tuple.Item4);
        Console.WriteLine("Value (Item5)= " + tuple.Item5);
        Console.WriteLine("Value (Item6)= " + tuple.Item6);
        Console.WriteLine("Value (Item7)= " + tuple.Item7);
        
        if (tuple.Item5 == 600) {
            Console.WriteLine("Exists: Tuple Item 5 = " + tuple.Item5);
        }
        if (tuple.Item6 == 900) {
            Console.WriteLine("Exists: Tuple Item 6 = " + tuple.Item6);
        }
        if (tuple.Item7 == 2000) {
            Console.WriteLine("Exists: Tuple Item 7 = " + tuple.Item7);
        }
        if (tuple.Item7 == tuple.Item6){
            Console.WriteLine("Tuple Items Matched!");
        }
    }
}

The output of the above code is −

Value (Item1)= 100
Value (Item2)= 150
Value (Item3)= 200
Value (Item4)= 300
Value (Item5)= 600
Value (Item6)= 1000
Value (Item7)= 1000
Exists: Tuple Item 5 = 600
Tuple Items Matched!

Conclusion

The 7-tuple in C# provides a convenient way to group seven related values of different types into a single object. It offers type safety, easy element access through Item1-Item7 properties, and can be created using either the constructor or the static Create method.

Updated on: 2026-03-17T07:04:35+05:30

189 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements