C# Program to access tuple elements

In C#, tuples are data structures that can hold multiple values of different types. Once you create a tuple, you can access its elements using the Item properties, where each element is numbered starting from Item1.

Syntax

Following is the syntax for creating a tuple using Tuple.Create()

var tupleName = Tuple.Create(value1, value2, value3, ...);

Following is the syntax for accessing tuple elements −

tupleName.Item1  // First element
tupleName.Item2  // Second element
tupleName.Item3  // Third element

Using Item Properties to Access Elements

Create a tuple with mixed data types and access each element using the Item properties −

using System;

public class Program {
    public static void Main() {
        var myTuple = Tuple.Create(1, 2.5M, "Amit", "100");
        Console.WriteLine("Item1 : " + myTuple.Item1);
        Console.WriteLine("Item2 : " + myTuple.Item2);
        Console.WriteLine("Item3 : " + myTuple.Item3);
        Console.WriteLine("Item4 : " + myTuple.Item4);
    }
}

The output of the above code is −

Item1 : 1
Item2 : 2.5
Item3 : Amit
Item4 : 100

Using Value Tuples (C# 7.0+)

C# 7.0 introduced value tuples with named elements, providing a more readable alternative −

using System;

public class Program {
    public static void Main() {
        var employee = (id: 101, salary: 5000.50, name: "John", department: "IT");
        
        Console.WriteLine("ID: " + employee.id);
        Console.WriteLine("Salary: " + employee.salary);
        Console.WriteLine("Name: " + employee.name);
        Console.WriteLine("Department: " + employee.department);
        
        // You can still use Item properties
        Console.WriteLine("Using Item1: " + employee.Item1);
    }
}

The output of the above code is −

ID: 101
Salary: 5000.5
Name: John
Department: IT
Using Item1: 101

Accessing Tuple Elements in Methods

You can pass tuples to methods and access their elements within the method −

using System;

public class Program {
    public static void DisplayTuple(Tuple<int, string, double> data) {
        Console.WriteLine("Processing tuple:");
        Console.WriteLine("Integer: " + data.Item1);
        Console.WriteLine("String: " + data.Item2);
        Console.WriteLine("Double: " + data.Item3);
    }
    
    public static void Main() {
        var myTuple = Tuple.Create(42, "Hello", 3.14);
        DisplayTuple(myTuple);
    }
}

The output of the above code is −

Processing tuple:
Integer: 42
String: Hello
Double: 3.14

Comparison: Tuple vs Value Tuple

Tuple (Reference Type) ValueTuple (Value Type)
Created using Tuple.Create() Created using () syntax
Elements accessed via Item1, Item2 Supports named elements and Item1, Item2
Reference type (heap allocation) Value type (stack allocation)
Available since .NET Framework 4.0 Available since C# 7.0

Conclusion

Tuple elements in C# are accessed using Item1, Item2, etc., properties based on their position. Value tuples (C# 7.0+) provide better performance and readability with named elements, making them the preferred choice for modern C# applications.

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

381 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements