How to get Seventh Element of the Tuple in C#?

A Tuple in C# is a data structure that can hold multiple values of different types. To access the seventh element of a tuple, you use the Item7 property. This property is available in tuples that have seven or more elements.

Syntax

Following is the syntax for accessing the seventh element of a tuple −

var seventhElement = tupleName.Item7;

For creating a tuple with seven elements −

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

Using Item7 Property

The Item7 property directly retrieves the seventh element from a tuple. Here's a focused example demonstrating how to access the seventh element −

using System;

public class Demo {
    public static void Main(string[] args) {
        var tuple1 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500);
        var tuple2 = Tuple.Create(75, 200, 500, 700, 100, 1200, 1500);
        
        Console.WriteLine("Tuple1 seventh element = " + tuple1.Item7);
        Console.WriteLine("Tuple2 seventh element = " + tuple2.Item7);
        
        Console.WriteLine("Are seventh elements equal? " + (tuple1.Item7 == tuple2.Item7));
        
        // Accessing other elements for comparison
        Console.WriteLine("Tuple1 first element = " + tuple1.Item1);
        Console.WriteLine("Tuple1 sixth element = " + tuple1.Item6);
    }
}

The output of the above code is −

Tuple1 seventh element = 1500
Tuple2 seventh element = 1500
Are seventh elements equal? True
Tuple1 first element = 75
Tuple1 sixth element = 1200

Working with 8-Element Tuples

When a tuple has more than seven elements, the eighth and subsequent elements are stored in the Rest property as a nested tuple −

using System;

public class Demo {
    public static void Main(string[] args) {
        var tuple = Tuple.Create(1200, 1500, 2200, 2700, 3100, 3500, 4500, 5500);
        
        Console.WriteLine("Tuple Item 1st = " + tuple.Item1);
        Console.WriteLine("Tuple Item 7th = " + tuple.Item7);
        
        // The 8th element is stored in Rest.Item1
        Console.WriteLine("Tuple Item 8th = " + tuple.Rest.Item1);
        
        Console.WriteLine("Type of Rest property: " + tuple.Rest.GetType().Name);
    }
}

The output of the above code is −

Tuple Item 1st = 1200
Tuple Item 7th = 4500
Tuple Item 8th = 5500
Type of Rest property: Tuple`1

Practical Example with Different Data Types

Tuples can store elements of different data types. Here's an example accessing the seventh element from a mixed-type tuple −

using System;

public class Demo {
    public static void Main(string[] args) {
        var studentInfo = Tuple.Create(
            "John",      // Item1: string
            25,          // Item2: int
            'A',         // Item3: char
            true,        // Item4: bool
            85.5,        // Item5: double
            "Computer Science", // Item6: string
            2024         // Item7: int (graduation year)
        );
        
        Console.WriteLine("Student Name: " + studentInfo.Item1);
        Console.WriteLine("Student Age: " + studentInfo.Item2);
        Console.WriteLine("Graduation Year: " + studentInfo.Item7);
        Console.WriteLine("Type of seventh element: " + studentInfo.Item7.GetType().Name);
    }
}

The output of the above code is −

Student Name: John
Student Age: 25
Graduation Year: 2024
Type of seventh element: Int32

Conclusion

The seventh element of a tuple in C# is accessed using the Item7 property. This property is available for tuples with seven or more elements, and it directly returns the value stored at the seventh position regardless of the data type.

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

165 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements