Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get Sixth Element of the Tuple in C#?
To access the sixth element of a Tuple in C#, you use the Item6 property. This property is available for tuples that contain six or more elements. Tuples in C# are immutable data structures that can store multiple values of different types.
Syntax
Following is the syntax to access the sixth element of a tuple −
var sixthElement = tuple.Item6;
The Item6 property returns the value stored at the sixth position in the tuple.
Using Item6 Property
The following example demonstrates how to create a 7-tuple and access its sixth element using the Item6 property −
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 1st = " + tuple1.Item1);
Console.WriteLine("Tuple2 Item 1st = " + tuple2.Item1);
Console.WriteLine("Tuple1 Item 2nd = " + tuple1.Item2);
Console.WriteLine("Tuple2 Item 2nd = " + tuple2.Item2);
Console.WriteLine("Tuple1 Item 4th = " + tuple1.Item4);
Console.WriteLine("Tuple2 Item 4th = " + tuple2.Item4);
Console.WriteLine("Tuple1 Item 5th = " + tuple1.Item5);
Console.WriteLine("Tuple2 Item 5th = " + tuple2.Item5);
Console.WriteLine("Tuple1 Item 6th = " + tuple1.Item6);
Console.WriteLine("Tuple2 Item 6th = " + tuple2.Item6);
Console.WriteLine("Tuple1 Item 7th = " + tuple1.Item7);
Console.WriteLine("Tuple2 Item 7th = " + tuple2.Item7);
}
}
The output of the above code is −
Is Tuple1 equal to Tuple2? = True HashCode of Tuple1 = 3231587 HashCode of Tuple2 = 3231587 Tuple1 Item 1st = 75 Tuple2 Item 1st = 75 Tuple1 Item 2nd = 200 Tuple2 Item 2nd = 200 Tuple1 Item 4th = 700 Tuple2 Item 4th = 700 Tuple1 Item 5th = 100 Tuple2 Item 5th = 100 Tuple1 Item 6th = 1200 Tuple2 Item 6th = 1200 Tuple1 Item 7th = 1500 Tuple2 Item 7th = 1500
Example with 8-Tuple
The following example shows an 8-tuple where we can still access the sixth element using Item6 −
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);
Console.WriteLine("Rest item (8th element) = " + tuple.Rest.Item1);
}
}
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 Rest item (8th element) = 5500
Accessing Sixth Element with Different Data Types
Tuples can store elements of different data types. Here's an example where the sixth element is a string −
using System;
public class Demo {
public static void Main(String[] args) {
var mixedTuple = Tuple.Create(10, "Hello", 3.14, true, 'A', "World", 100);
Console.WriteLine("Sixth element: " + mixedTuple.Item6);
Console.WriteLine("Sixth element type: " + mixedTuple.Item6.GetType().Name);
// Demonstrating all elements
Console.WriteLine("All elements:");
Console.WriteLine("Item1 (int): " + mixedTuple.Item1);
Console.WriteLine("Item2 (string): " + mixedTuple.Item2);
Console.WriteLine("Item3 (double): " + mixedTuple.Item3);
Console.WriteLine("Item4 (bool): " + mixedTuple.Item4);
Console.WriteLine("Item5 (char): " + mixedTuple.Item5);
Console.WriteLine("Item6 (string): " + mixedTuple.Item6);
Console.WriteLine("Item7 (int): " + mixedTuple.Item7);
}
}
The output of the above code is −
Sixth element: World Sixth element type: String All elements: Item1 (int): 10 Item2 (string): Hello Item3 (double): 3.14 Item4 (bool): True Item5 (char): A Item6 (string): World Item7 (int): 100
Conclusion
The sixth element of a Tuple in C# is accessed using the Item6 property. This property is available for tuples containing six or more elements and returns the value stored at the sixth position, regardless of the data type of that element.
