C# Tuple

A tuple is a data structure in the C# language. A tuple is known as the data structure that contains a sequence of elements having different data types.

Syntax:
Characteristics of Tuples:
  • C# Tuple can store duplicate elements.
  • Tuple allows to create, access and manipulate data set.
  • Tuple also allows to represent multiple data into a single data set.
  • Tuple return multiple values from a method without using out parameter.

Creating C# Tuple:

In C#, there are mainly 2 ways of creating C# Tuple which are as follows:

  1. Using Constructor of Tuple Class
  2. Using Create Method
  • Using Constructor of Tuple Class: 

There is a constructor provided by Tuple<T> class, which is used to create the tuple class. This constructor is used to store the elements starting from 1 to 8 with their type, but can’t store more than 8 in a tuple, then the compiler will throw an error.

Syntax:
Example:
  • Using Create Method:

If the above mentioned constructor is used we have to create the type of each element stored in the tuple, which makes the code quite lengthy. But there is another class in C# that contains the static methods for creating tuple objects without providing the type of each element. It makes the code less confusing.

Syntax:
Example:

Accessing C# Tuple:

The elements of a C# tuple can be accessed by using the Item<elementNumber> property. Elementnumber can be written from 1 to 7.

// C# program to access the tuple
// using Item and Rest property
using System;

public class T{

	// Main method
	static public void Main()
	{

		// Creating tuple1
		// Using Create Method
		var Tuple1 = Tuple.Create("C# Tuple");

		// Accessing the element of Tuple
		// Using Item property
		Console.WriteLine("Members of Tuple1: " + Tuple1.Item1);
		Console.WriteLine();

		// Creating 4-tuple
		// Using Create Method
		var Tuple2 = Tuple.Create(89, 90);

		// Accessing the element of Tuple
		// Using Item property
		Console.WriteLine("Element of My_Tuple2: " + Tuple2.Item1);
		Console.WriteLine("Element of My_Tuple2: " + Tuple2.Item2);
		Console.WriteLine();		
	}
}

Nested Tuple:

In C# Tuple we can create a tuple inside another tuple. For this purpose, Nested Tuple is was used. Through which we can add more than 8 elements in the same tuple.

Example:

// C# program to illustrate nested tuple
using System;
  
public class T{
      
        // Main method
    static public void Main ()
        {
          
           // Nested Tuple
        var Tuple1 = Tuple.Create(10, "Tuple", 20,
             Tuple.Create(12, 25));
          
        // Accessing the element of Tuple
        // Using Item property
        // And accessing the elements of nested tuple
        // Using Rest property
        Console.WriteLine("Values of Tuple: "+Tuple1.Item1);
        Console.WriteLine("Values of Tuple: "+Tuple1.Item2);
        Console.WriteLine("Values of Tuple: "+Tuple1.Item3);
        Console.WriteLine("Values of Nested tuple: "+Tuple1.Rest);
        Console.WriteLine("Values of Nested tuple: "+Tuple1.Rest.Item1.Item1);
        Console.WriteLine("Values of Nested tuple: "+Tuple1.Rest.Item1.Item2);
	}
}

Tuple as a Return Type:

Methods are allowed to use a tuple as a return type in C# programing. It can be considered as a method can return a tuple. Given example below show it better.

// how a method return tuple in C#
using System;

public class T{
	
		// Main Method
	static public void Main ()
		{
			// Return tuple is stored in mytuple
		var tuple1 = PrintTuple();
		Console.WriteLine(tuple1.Item1);
		Console.WriteLine(tuple1.Item2);
	
	}
	
	// PrintTuple method return a tuple
	static Tuple<string, string>PrintTuple()
		{
		return Tuple.Create("C#", "Tuple");
	}
}

Tuple as a Method Parameter:

In C# tuple can be passed as a method parameter. Given example below show it better.

// C# tuple program as a method parameter

using System;

public class T{
	
		// Main method
	static public void Main ()
		{
		
			// Creating a tuple
		var tuple1 = Tuple.Create("C# Tuple", 999, 8.8 );
		
		// Pass the tuple in the
				// PrintTheTuple method
		PrintTheTuple(tuple1);
	}

	static void PrintTheTuple(Tuple<string, int, double>tuple1)
		{
		Console.WriteLine("Value1: "+tuple1.Item1);
		Console.WriteLine("Value2: "+tuple1.Item2);
		Console.WriteLine("Value3: "+tuple1.Item3);
	}
}