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
Selected Reading
How to get Fourth Element of the Tuple in C#?
To get the fourth element of a Tuple in C#, you use the Item4 property. This property provides direct access to the fourth item stored in the tuple, regardless of the tuple's total size.
Syntax
Following is the syntax for accessing the fourth element of a tuple −
var fourthElement = tuple.Item4;
The Item4 property returns the value stored at the fourth position in the tuple.
Using Item4 with Different Tuple Types
Example with 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 1st = " + tuple1.Item1);
Console.WriteLine("Tuple1 Item 4th = " + tuple1.Item4);
Console.WriteLine("Tuple2 Item 4th = " + tuple2.Item4);
Console.WriteLine("Tuple1 Item 5th = " + tuple1.Item5);
}
}
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 Tuple1 Item 4th = 700 Tuple2 Item 4th = 700 Tuple1 Item 5th = 100
Example with 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 Item4 with Mixed Data Types
Example
using System;
public class Demo {
public static void Main(String[] args) {
var mixedTuple = Tuple.Create("John", 25, true, 3.14, "Engineer");
Console.WriteLine("Name (Item1): " + mixedTuple.Item1);
Console.WriteLine("Age (Item2): " + mixedTuple.Item2);
Console.WriteLine("Is Active (Item3): " + mixedTuple.Item3);
Console.WriteLine("Pi Value (Item4): " + mixedTuple.Item4);
Console.WriteLine("Profession (Item5): " + mixedTuple.Item5);
// Accessing fourth element specifically
double piValue = mixedTuple.Item4;
Console.WriteLine("Fourth element as double: " + piValue);
}
}
The output of the above code is −
Name (Item1): John Age (Item2): 25 Is Active (Item3): True Pi Value (Item4): 3.14 Profession (Item5): Engineer Fourth element as double: 3.14
Conclusion
The Item4 property provides direct access to the fourth element of any tuple in C#. This property is strongly typed and returns the exact type of the fourth element as specified when creating the tuple, making it safe and efficient for accessing tuple data.
Advertisements
