C# | Tuples

C# tuple is a data structure in C#. In this article, learn how to use tuples in C#. Tuples in C# are used to return multiple data values. 

If you need to return multiple values from a method in C#, there are three ways to do so without using tuples. 

  1. Using 0ut parameters
  2. Using a class, struct, or a record type
  3. Anonymous types returned through a dynamic return type 

In C#, Tuples solve this problem. C# tuple is a data structure that provides an easy way to represent a single set of data. The System.Tuple class provides static methods to create tuple objects.

Tuples allow us to,

  • Create, access, and manipulate a data set
  • Return a data set from a method without using the out parameter
  • Pass multiple values to a method through a single parameter 

C# Tuple

We can create a Tuple<> using its constructor or the "Create" method. The code snippet in Listing 1 creates a 3-tuple using a constructor. The tuple is a set of three data types, including two strings and one int, that represents an author's name, book title, and year of publication.

// Create a 3-tuple
var author = new Tuple<string, string, int>("Mahesh Chand", "ADO.NET Programming", 2003);

 // Display author info
System.Console.WriteLine("Author {0} wrote his first book titled {1} in {2}.", author.Item1, author.Item2, author.Item3);
C#
Listing 1.

The code snippet in Listing 2 creates a 5-tuple using the static "Create" method. The tuple is a set of five data types: three strings, one int, and one double data type.

// Create a 5-tuple
var pubAuthor = Tuple.Create("Mahesh Chand", "Graphics Programming with GDI+", "Addison Wesley", 2004, 49.95);
System.Console.WriteLine("Author {0} wrote his fourth book titled {1} for {2} in {3}. Price: {4}",
pubAuthor.Item1, pubAuthor.Item2, pubAuthor.Item3, pubAuthor.Item4, pubAuthor.Item5);
C#

Listing 2.

How to create nested tuples in C#?

C# supports tuples with up to seven elements. To have a tuple with more than seven elements, you can use the 8th element, TRest, to create nesting tuple objects. The code snippet in Listing 3 creates a tuple with a nested tuple inside it.

var even8 = new Tuple<int, int, int, int, int, int, int, Tuple<double, double, double>> (2, 4, 6, 8, 10, 12, 14, Tuple.Create(1.1,1.2,1.3));
Console.WriteLine("{0},{1},{2}", even8.Rest.Item1, even8.Rest.Item2, even8.Rest.Item3);
C#

Listing 3.

How to use a tuple in a method?

A tuple is useful when you need to pass a data set as a single parameter of a method without using ref and out parameters. The code snippet in Listing 4 passes a tuple as a parameter of the method.

public void SetTupleMethod(Tuple < string, string, int > tupleAuthor) {
    var author2 = tupleAuthor;
    Console.WriteLine("Author:{0}, Title:{1}, Year:{2}.", author2.Item1, author2.Item2, author2.Item3);
}
C#

Listing 4. 

The following code snippet in Listing 5 calls the method.

ts.SetTupleMethod(new Tuple<string, string, int>(
"Mike Gold", "Code UML", 2005));
C#

Listing 5.

How to return tuples in C#?

A tuple can be used to return a data set as a single variable of a method. The code snippet in Listing 6 returns a tuple with three values.

public static Tuple < string, string, int > GetTupleMethod() {
    // Create a 3-tuple and return it
    var author = new Tuple < string,
        string, int > ("Mahesh Chand", "Programming C#", 2002);
    return author;
}
C#

Listing 6.

The code snippet in Listing 7 calls the method, gets a tuple and reads its values.

var author2 = TupleSamples.GetTupleMethod();
Console.WriteLine("Author:{0}, Title:{1}, Year:{2}.", author2.Item1, author2.Item2, author2.Item3);
C#

Comments

Popular posts from this blog

C# | Association, Aggregation and Composition

Throw vs Throw ex in C#

C# Extension Method