Tuples In C#

Often, we want to return more than one value from a class method. Prior to the introduction of tuples in .NET, there were three common ways to do so. 

  • Out parameters
  • Class or struct types
  • Anonymous types returned through a dynamic return type 

Tuples solve this problem. Tuples aren’t new to C# or .NET. Tuples were first introduced as a part of .NET Framework 4.0.

A 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 out parameter
  • Pass multiple values to a method through a single parameter 

Create and Access Tuples

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 3 data types including two strings and one int that represents an author's name, book title, and year of publication.

  1. // Create a 3-tuple  
  2. var author = new Tuple<stringstringint>("Mahesh Chand""ADO.NET Programming", 2003);  
  3.   
  4. // Display author info  
  5. System.Console.WriteLine("Author {0} wrote his first book titled {1} in {2}.", author.Item1, author.Item2, author.Item3);   

Listing 1.

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

  1. // Create a 5-tuple  
  2. var pubAuthor = Tuple.Create("Mahesh Chand""Graphics Programming with GDI+""Addison Wesley", 2004, 49.95);  
  3.   
  4. 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);  
Listing 2.
Nested Tuples

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

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

Listing 3.

Tuples in methods

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.

  1. public void SetTupleMethod(Tuple<stringstringint> tupleAuthor)  
  2. {  
  3. var author2 = tupleAuthor;  
  4. Console.WriteLine("Author:{0}, Title:{1}, Year:{2}.",  
  5. author2.Item1, author2.Item2, author2.Item3);  
  6. }  
Listing 4.

The following code snippet in Listing 5 calls the method.

  1. ts.SetTupleMethod(new Tuple<stringstringint>(  
  2. "Mike Gold""Code UML", 2005));  

Listing 5.

Return Tuples

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 3 values.

  1. public static Tuple<stringstringint> GetTupleMethod()  
  2. {  
  3. // Create a 3-tuple and return it  
  4. var author = new Tuple<stringstringint>(  
  5. "Mahesh Chand""Programming C#", 2002);  
  6. return author;  
  7. }  

Listing 6.

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

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

Listing 7.

C# 7.0 and Tuples

Note

C# 7.0 features may not be available as a part of Visual Studio 2017 RC or other previous versions. To add the Tuples feature, you may want to download and install the NuGet package.

Step 1

Right click on the project name in Solution Explorer and select “Manage NuGet Package”.

Step 2

Click on the "Browse" tab and type System.ValueTuple in the TextBox. You will see the package name, as shown below.

 

Step 3

Select “TuplesSample” and click on the "Install" button.

Follow the instructions.

You’re now ready to use C# 7.0 tuples.

C# 7.0 extends the tuples' functionality by adding tuple types and tuple literals.

We can replace the above tuples code sample with the code listed in Listing 8, where the TupleReturnLiteral method returns a tuple type of three values.

  1. // tuple return type  
  2. public (stringstringlong) TupleReturnLiteral(long id)  
  3. {  
  4.   
  5. string name = string.Empty;  
  6. string title = string.Empty;  
  7. long year = 0;  
  8.   
  9. if (id == 1000)  
  10. {  
  11. name = "Mahesh Chand";  
  12. title = "ADO.NET Programming";  
  13. year = 2003;  
  14. }  
  15.   
  16. // tuple literal  
  17. return (name, title, year);  
  18.   
  19. }  

Listing 8.

The code listed in Listing 9 calls the above TupleReturnLiternal method and returns a tuple variable. The code reads the tuple values using Item1, Item2, and Item3 of tuple and displays the values on the console.

  1. TupleSamples ts = new TupleSamples();  
  2. var author = ts.TupleReturnLiteral(1000);  
  3. Console.WriteLine($"Author {author.Item1} {author.Item2} {author.Item3} ");  

Listing 9.

To make the above code more readable, we can name the tuple return type values. The code snippet in Listing 10 changes the method signature by giving the tuple type values names.

  1. // tuple return type  
  2. public (string name, string title, long year) TupleReturnLiteral(long id)  
  3. {  
  4.   
  5. string name = string.Empty;  
  6. string title = string.Empty;  
  7. long year = 0;  
  8.   
  9. if (id == 1000)  
  10. {  
  11.   
  12. name = "Mahesh Chand";  
  13. title = "ADO.NET Programming";  
  14. year = 2003;  
  15. }  
  16.   
  17. // tutle literal  
  18. return (name, title, year);  
  19.   
  20. }  

Listing 10.

The code snippet in Listing 11 calls the method listed in Listing 10. As you can see from Listing 11, the tuple values can be accessed using the tuple variable names.

  1. TupleSamples ts = new TupleSamples();  
  2. var author = ts.TupleReturnLiteral(1000);  
  3. Console.WriteLine($"Author {author.name} {author.title} {author.year} ");  

Listing 11.

Summary

In this article, we learned about tuples in C# and how to apply the new changes introduced in C# 7.0.

Next C# 7.0 Feature >> Out Variable In C# 7.0

References

References used to write this article,

https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/

Up Next
    Ebook Download
    View all
    Learn
    View all