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

Introduction

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

static (string, string, int) MyData()  
{  
    return ("Omar", "maher", 123456);  
}  

 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.

static (string, string, int) MyData()  
{  
    return ("Omar", "maher", 123456);  
}   

Now, let’s call our method.

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

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.

static (string firstName, string lastName, int password) MyData2()  
{  
    return ("Omar", "maher", 123456);  
}   

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

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

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

C#

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

static void Main(string[] args)  
{  
    var userData2 = MyData2();  
    Console.WriteLine($"My name is { userData2.firstName} " +  
        $"{ userData2.lastName} and my password is " +  
        $"{ userData2.password}");  
  
    Console.WriteLine($"My name is { userData2.Item1} " +  
        $"{ userData2.Item2} and my password is " +  
        $"{ userData2.Item3}");  
  }   

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.

static void Main(string[] args)  
{  
   (var firstName, var lastName, var password) = MyData2();  
  
    Console.WriteLine($"My name is {firstName} " +  
         $"{ lastName} and my password is " +  
         $"{ password}");  
 } 

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

C#

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

static void Main(string[] args)  
        {  
            var (firstName, lastName, password) = MyData2();  
  
            Console.WriteLine($"My name is {firstName} " +  
                 $"{ lastName} and my password is " +  
                 $"{ password}");  
          }   

And, the result also will be the same.

C#

static void Main(string[] args)  
        {  
            var (firstName, lastName, password) = MyData2();  
  
            Console.WriteLine($"My name is {firstName} " +  
                 $"{ lastName} and my password is " +  
                 $"{ password}");  
          }   

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

In the end, I hope it will help you.


Similar Articles