VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/c-sharp-tuple/

⇱ C# Tuple - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C# Tuple

Last Updated : 21 Jan, 2026

A tuple is a data structure which has multiple values of different types. It was introduced in .NET Framework 4.0. In a tuple, we can add elements from 1 to 8. If try to add elements greater than eight, then the compiler will throw an error. Tuples are generally used when we want to create a data structure which contains objects with their properties and don’t want to create a separate type for that.

Features of Tuples

  • It allows us to represent multiple data into a single data set.
  • It allows us to create, manipulate, and access data sets.
  • It returns multiple values from a method without using out parameter.
  • It can also store duplicate elements.
  • It allows us to pass multiple values to a method with the help of single parameters.

Example:


Output
123
Hello
True

Before tuples, we have three ways to return more than one value from the method in C# which are Using Class or Struct types, Out parameters and Anonymous types which are returned through a Dynamic Return Type. But after Tuples, it becomes easy to represent a single set of data. There is no need to create any separate data structure. Instead, for this.

Creating Tuple

In C#, there are mainly 2 ways to create the tuple

1. Value Tuples (Modern and Recommended)

Value tuples were introduced in C# 7.0 and are implemented using the System.ValueTuple structure.

Syntax:

(type1, type2, ..., typeN) tupleName;
(type1 name1, type2 name2) tupleName;

Example:


Output
123
Hello
True

2. Reference Tuples (Tuple<T>) (Legacy)

Reference tuples were introduced in .NET Framework 4.0 and are implemented using the Tuple<T> class.

Syntax:

Tuple<T1, T2, ....> tupleName;

1. Using Constructor


Output
GeeksforGeeks
123
90.8

2. Using Tuple.Create()


Output
C#
Python
29

Accessing a Tuple

We can access the elements of a tuple by using the Item<elementNumber> property, here elementNumber is from 1 to 7 like Item1, Item 2, Item3, Item4, Item5, Item6, Item 7, etc. and the last element of 8-tuple is accessible by using Rest property as shown in the below example:

Example:


Output
1st element of t1 : GeeksforGeeks

1st element of t2: 12

1st element of t3: 13
2nd lement of t3: Geeks
8th element of t3: (10)

Nested Tuples

In C#, we can create a tuple into another tuple we use nested tuples when you want to add more than eight elements in the same tuple. The nested tuple is accessible by using the Rest property as shown below example. We can add a nested tuple anywhere in the sequence, but it is recommended that place a nested tuple at the end of the sequence so that they can easily access from the Rest property.

Example 1:


Output
Element of My_Tuple: 13
Element of My_Tuple: Geeks
Element of Nested tuple: ((12, 30, 40, 50))
Element of Nested tuple: 12

Example 2: Accessing nested tuple other than the last place


Output
1st element of tuple: 13
1st Element of Nested Tuple: 12
2nd Element of Nested Tuple: 30
3rd Element of Nested Tuple: 40
4th Element of Nested Tuple: 50

Tuple as a Method Parameter

In C#, we are allowed to pass a tuple as a method parameter as shown in the below example. Here we pass a tuple named mytuple in the PrintTheTuple() method and the elements of the tuple are accessed by using the Item<elementNumber> property.

Example:


Output
Element: GeeksforGeeks
Element: 123
Element: 90.8

Tuple as a Return Type

In C#, methods are allowed to use a tuple as a return type. In other words, a method can return a tuple as shown in the below example:

Example:


Output
Geeks
For
Geeks

Limitations of Tuple

  • It is of reference type not of value type.
  • It is limited to eight elements. This means you cannot store more than eight elements without a nested tuple.
  • These are only accessed by using the Item<elementNumber> property.
Comment
Article Tags:
Article Tags:

Explore