Home Basics C# Tuple

C# Tuple

A Tuple is a data structure, and it is useful to store the sequence of elements of different data types. Using a tuple, we can return more than one value from the methods as a single set of data.

Tuples were introduced in .NET Framework 4.0.

It can be used where you want to have a data structure to hold an object with properties, but you don’t want to create a separate type for it.

tuples are useful in the following scenarios.

  • To represent or return different data type elements as a single set of data.
  • To return multiple values from methods without using ref or out parameters.
  • To pass multiple values to a method with a single parameter.
  • Temporarily hold multiple values without creating a class or struct.

Here is the syntax for a tuple

Tuple <T1, T2, T3, T4, T5, T6, T7, TRest>

Tuple creation

We can create a Tuple<> using its constructor or the “Create” method

using System;

public class Program
{
	public static void Main()
	{
		// Create a 3-tuple  
		var footballer = new Tuple<string, string, int>("Lionel Messi", "Barcelona", 2021);  
  
		// Display author info  
		System.Console.WriteLine("{0} signed his new contract with {1} in {2}.", footballer.Item1, footballer.Item2, footballer.Item3);
		Console.WriteLine("Press Enter Key to Exit..");
        Console.ReadLine();
	}
}

The next code snippet creates a tuple using the static “Create” method.

using System;

public class Program
{
	public static void Main()
	{
		// Create a 3-tuple  
  		var footballer = Tuple.Create("Lionel Messi", "Barcelona", 2021);  
		// Display author info  
		System.Console.WriteLine("{0} signed his new contract with {1} in {2}.", footballer.Item1, footballer.Item2, footballer.Item3);
		Console.WriteLine("Press Enter Key to Exit..");
        Console.ReadLine();
	}
}

You will see the following

Lionel Messi signed his new contract with Barcelona in 2021.
Press Enter Key to Exit..

Access Elements

we can access the tuple object elements with Item<positionNumber> properties.

using System;

public class Program
{
	public static void Main()
	{
		// Create a 3-tuple  
		var numbers = Tuple.Create(1, 2, 3, 4, 5, 6, 7, 8);
		var item1 = numbers.Item3; // 3
		var item2 = numbers.Item4; // 4
		var item3 = numbers.Rest; // (8)
		var item4 = numbers.Rest.Item1; // 8 
		System.Console.WriteLine(item1);
		System.Console.WriteLine(item2);
		System.Console.WriteLine(item3);
		System.Console.WriteLine(item4);
		Console.WriteLine("Press Enter Key to Exit..");
        Console.ReadLine();
	}
}

You will see this

3
4
(8)
8
Press Enter Key to Exit..

Nested Tuples

A Tuple will directly support only 8 elements. If we want to store more than 8 elements, then we need to use nested tuples.

using System;

public class Program
{
	public static void Main()
	{
          var numbers = Tuple.Create(1, 2, 3, 4, 5, 6, 7, Tuple.Create(8, 9, 10, 11, 12));
          Console.WriteLine("*****Numbers*****");
          Console.WriteLine("{0}", numbers.Item2);
          Console.WriteLine("{0}", numbers.Item5);
          Console.WriteLine("{0}", numbers.Rest.Item1);
          Console.WriteLine("{0}", numbers.Rest.Item1.Item2);
          Console.WriteLine("{0}", numbers.Rest.Item1.Item3);
          Console.ReadLine();
	}
}

Return Type

A Tuple can be returned from a method.

 

using System;

public class Program
{
	public static void Main()
	{
		var person = Footballer();
		Console.WriteLine(person);
	}

	static Tuple<int, string, string> Footballer() 
	{
		return Tuple.Create(1, "Lionel", "messi");
	}
}

Tuple as a Method Parameter

A method can have a tuple as a parameter.

 

 

Note

The Tuple is a reference type and not a value type. It allocates on heap and could result in CPU intensive operations.

The Tuple is limited to include eight elements. You need to use nested tuples if you need to store more elements.

The Tuple elements can be accessed using properties with a name pattern Item<elementNumber>, which does not make sense.

You may also like