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

In C#, you can access the fifth element of a tuple using the Item5 property. This property is available for tuples that contain at least five elements. The Item5 property returns the value stored at the fifth position in the tuple.

Syntax

Following is the syntax to access the fifth element of a tuple −

var fifthElement = tupleVariable.Item5;

Where tupleVariable is a tuple with at least five elements, and Item5 retrieves the value at the fifth position.

Using Item5 Property

Example 1

The following example demonstrates how to access the fifth element of a 7-tuple −

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("Is Tuple1 equal to Tuple2? = " + tuple1.Equals(tuple2));
        Console.WriteLine("HashCode of Tuple1 = " + tuple1.GetHashCode());
        Console.WriteLine("HashCode of Tuple2 = " + tuple2.GetHashCode());
        Console.WriteLine("Tuple1 Item 5th = " + tuple1.Item5);
        Console.WriteLine("Tuple2 Item 5th = " + tuple2.Item5);
    }
}

The output of the above code is −

Is Tuple1 equal to Tuple2? = True
HashCode of Tuple1 = 3231587
HashCode of Tuple2 = 3231587
Tuple1 Item 5th = 100
Tuple2 Item 5th = 100

Example 2

This example shows accessing all elements including the fifth element from an 8-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("HashCode of Tuple = " + tuple.GetHashCode());
        Console.WriteLine("Tuple Item 1st = " + tuple.Item1);
        Console.WriteLine("Tuple Item 2nd = " + tuple.Item2);
        Console.WriteLine("Tuple Item 3rd = " + tuple.Item3);
        Console.WriteLine("Tuple Item 4th = " + tuple.Item4);
        Console.WriteLine("Tuple Item 5th = " + tuple.Item5);
        Console.WriteLine("Tuple Item 6th = " + tuple.Item6);
        Console.WriteLine("Tuple Item 7th = " + tuple.Item7);
    }
}

The output of the above code is −

HashCode of Tuple = 49989024
Tuple Item 1st = 1200
Tuple Item 2nd = 1500
Tuple Item 3rd = 2200
Tuple Item 4th = 2700
Tuple Item 5th = 3100
Tuple Item 6th = 3500
Tuple Item 7th = 4500

Using Value Tuples (Modern Approach)

C# 7.0 introduced value tuples which provide a more modern and efficient way to work with tuples −

using System;

public class Demo {
    public static void Main(String[] args) {
        var valueTuple = (10, 20, 30, 40, 50, 60, 70);
        
        Console.WriteLine("Fifth element using Item5: " + valueTuple.Item5);
        
        // Named tuple elements for better readability
        var namedTuple = (first: 100, second: 200, third: 300, fourth: 400, fifth: 500);
        Console.WriteLine("Fifth element using name: " + namedTuple.fifth);
        Console.WriteLine("Fifth element using Item5: " + namedTuple.Item5);
    }
}

The output of the above code is −

Fifth element using Item5: 50
Fifth element using name: 500
Fifth element using Item5: 500

Conclusion

The Item5 property provides direct access to the fifth element of a tuple in C#. Both classic tuples and value tuples support this property, making it easy to retrieve the fifth element from tuples containing five or more elements.

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

163 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements