Tuples In C# 7 - What Are They And How Can I Use Them?

Tuples are lightweight data structures that contain multiple fields to represent the data members. To return more than one variable, the traditional way is using out parameter but there are some limitations for out parameter, like we can’t use it with async method.

  • Tuples are value types not reference type.
  • Tuple elements are public and mutable fields.
  • You can convert tuples to other tuples.
  • Tuple is not built in visual studio but you should install it as nuGet package
  • Tuple can be return type and also can be literal type.
  • Tuple can have two values or more.

Let’s try by examples.

Create a console app and give it the name Tuples.

C#

Now, we will write our method and you can access them by . (dot), then item1 , item2 ,etc..

First way to use tuple is to just write types of the variable we will return 
  1. static (string, string, int) MyData()  
  2. {  
  3.     return ("Omar""maher", 123456);  
  4. }   

Now, you have red line because tuple is not built in Visual Studio. Right click on the project and click on "Manage NuGet Packages" and type System.ValueTuple.

C#

Install it. Then back to the console app, you will find no red lines.

  1. static (string, string, int) MyData()  
  2. {  
  3.     return ("Omar""maher", 123456);  
  4. }   

Now, let’s call our method.

  1. static void Main(string[] args)  
  2. {  
  3.     var userData = MyData();  
  4.     Console.WriteLine($"My name is { userData.Item1} " +  
  5.         $"{ userData.Item2} and my password is " +  
  6.         $"{ userData.Item3}");  
  7.           }   

Here, userData.Item1 refers to first variable and userData.Item2 refers to the second variable, and userData.Item3 refers to the third variable. The following will be the result when you press F5.

C#

Second way to use tuple is to give variables a name

Let's create new MyData2() method and give every variable a name.

  1. static (string firstName, string lastName, int password) MyData2()  
  2. {  
  3.     return ("Omar""maher", 123456);  
  4. }   

And now, let’s call it with the the same previous example like this.

  1. static void Main(string[] args)  
  2. {  
  3.     var userData2 = MyData2();  
  4.     Console.WriteLine($"My name is { userData2.Item1} " +  
  5.         $"{ userData2.Item2} and my password is " +  
  6.         $"{ userData2.Item3}");  
  7.  }   

And when it runs, it will give us the same result.

C#

And also, you can use the name of variables like this.

  1. static void Main(string[] args)  
  2. {  
  3.     var userData2 = MyData2();  
  4.     Console.WriteLine($"My name is { userData2.firstName} " +  
  5.         $"{ userData2.lastName} and my password is " +  
  6.         $"{ userData2.password}");  
  7.   
  8.     Console.WriteLine($"My name is { userData2.Item1} " +  
  9.         $"{ userData2.Item2} and my password is " +  
  10.         $"{ userData2.Item3}");  
  11.   }   

As you see in the above example, we used both the ways for the same MyData2() method by .Item1 and by the name of the variable. Let's run it and see the results.

C#

Wow, we got the same results.

Third way to consume tuple is by Deconstructions

That means I will create new variables to assign to its values from the return of the method, like this.

  1. static void Main(string[] args)  
  2. {  
  3.    (var firstName, var lastName, var password) = MyData2();  
  4.   
  5.     Console.WriteLine($"My name is {firstName} " +  
  6.          $"{ lastName} and my password is " +  
  7.          $"{ password}");  
  8.  }   

And, let's run it to see the result.

C#

Or in a short way, by using one var before it.

  1. static void Main(string[] args)  
  2.         {  
  3.             var (firstName, lastName, password) = MyData2();  
  4.   
  5.             Console.WriteLine($"My name is {firstName} " +  
  6.                  $"{ lastName} and my password is " +  
  7.                  $"{ password}");  
  8.           }   

And, the result also will be the same.

C#

You can find the source code here - https://github.com/omarmaher100/Tuples

In the end, I hope it will help you.