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 Third Element of the Tuple in C#?
To get the third element of a Tuple in C#, you use the Item3 property. Tuples in C# provide indexed properties (Item1, Item2, Item3, etc.) to access their elements by position.
Syntax
Following is the syntax to access the third element of a tuple −
var thirdElement = tuple.Item3;
You can also create tuples using the generic Tuple<T1, T2, T3, ...> class or the Tuple.Create() method −
var tuple1 = new Tuple<int, int, int>(10, 20, 30); var tuple2 = Tuple.Create(10, 20, 30);
Using Item3 Property
The Item3 property directly returns the third element of the tuple. Here's a focused example −
using System;
public class Demo {
public static void Main(String[] args) {
var tuple = Tuple.Create(100, 200, 300, 400, 500);
Console.WriteLine("First element: " + tuple.Item1);
Console.WriteLine("Second element: " + tuple.Item2);
Console.WriteLine("Third element: " + tuple.Item3);
// Accessing third element in a variable
int thirdElement = tuple.Item3;
Console.WriteLine("Third element stored in variable: " + thirdElement);
}
}
The output of the above code is −
First element: 100 Second element: 200 Third element: 300 Third element stored in variable: 300
Working with Different Data Types
Tuples can store elements of different data types. Here's an example accessing the third element from a mixed-type tuple −
using System;
public class Demo {
public static void Main(String[] args) {
var mixedTuple = Tuple.Create("John", 25, true, 85.5, "Engineer");
Console.WriteLine("Name: " + mixedTuple.Item1);
Console.WriteLine("Age: " + mixedTuple.Item2);
Console.WriteLine("Is Active: " + mixedTuple.Item3);
Console.WriteLine("Score: " + mixedTuple.Item4);
Console.WriteLine("Profession: " + mixedTuple.Item5);
// Accessing third element specifically
bool isActive = mixedTuple.Item3;
Console.WriteLine("Third element (Is Active): " + isActive);
}
}
The output of the above code is −
Name: John Age: 25 Is Active: True Score: 85.5 Profession: Engineer Third element (Is Active): True
Tuple with Seven Elements
Here's an example demonstrating access to the third element in a larger tuple −
using System;
public class Demo {
public static void Main(String[] args) {
var tuple = Tuple.Create(1200, 1500, 2200, 2700, 3100, 3500, 4500);
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);
// Highlighting third element access
Console.WriteLine("The third element is: " + tuple.Item3);
}
}
The output of the above code is −
HashCode of Tuple = -737223690 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 The third element is: 2200
Conclusion
Accessing the third element of a tuple in C# is straightforward using the Item3 property. This indexed approach works consistently regardless of the tuple's size or the data types it contains, making it easy to retrieve specific elements by their position.
