C# - Tuple

 Admin   
193 View    Jan 20 2020 11:08PM

C# - Tuple

The Tuple<T> class was presented in .NET Framework 4.0. A tuple is an information structure that contains a succession of components of various information types. It very well may be utilized where you need to have an information structure to hold an item with properties, yet you would prefer not to make a different kind for it.

Tuple<T1, T2, T3, T4, T5, T6, T7, TRest>

 

The following example creates a tuple with three elements:

Tuple<int, string, string> person = 
                        new Tuple <int, string, string>(1, "Steve", "Jobs")

 

In the above model, we made an occurrence of the Tuple which holds a record of an individual. We determined a sort for every component and passed qualities to the constructor. Indicating the kind of every component is unwieldy. In this way, C# incorporates a static assistant class Tuple which restores a case of the Tuple<T> without indicating the kind of every component, as demonstrated as follows.

 

var person = Tuple.Create(1, "Steve", "Jobs");
 

A tuple can only include maximum eight elements. It gives a compiler error when you try to include more than eight elements.

var numbers = Tuple.Create(1, 2, 3, 4, 5, 6, 7, 8);
 

 

Getting to Tuple Elements

 

The components of a tuple can be gotten to with Item<elementNumber> properties for example Item1, Item2, Item3, etc up to Item7 property. The Item1 property restores the primary component, Item2 restores the subsequent component, etc. The last component (the eighth component) will be returned utilizing the Rest property.

 

Example: Accessing Tuple Elements

 

var person = Tuple.Create(1, "Steve", "Jobs");
person.Item1; // returns 1
person.Item2; // returns "Steve"
person.Item3; // returns "Jobs"
var numbers = Tuple.Create("One", 2, 3, "Four", 5, "Six", 7, 8);
numbers.Item1; // returns "One"
numbers.Item2; // returns 2
numbers.Item3; // returns 3
numbers.Item4; // returns "Four"
numbers.Item5; // returns 5
numbers.Item6; // returns "Six"
numbers.Item7; // returns 7
numbers.Rest; // returns (8)
numbers.Rest.Item1; // returns 8
 

 

Generally, the 8th position is for the nested tuple which you can access using the Rest property.

Nested Tuples

If you want to include more than eight elements in a tuple, you can do that by nesting another tuple object as the eighth element. The last nested tuple can be accessed using the Rest property. To access the nested tuple's element, use the Rest.Item1.Item<elelementNumber> property.

Example: Nested Tuple

var numbers = Tuple.Create(1, 2, 3, 4, 5, 6, 7, Tuple.Create(8, 9, 10, 11, 12, 13));
numbers.Item1; // returns 1
numbers.Item7; // returns 7
numbers.Rest.Item1; //returns (8, 9, 10, 11, 12, 13)
numbers.Rest.Item1.Item1; //returns 8
numbers.Rest.Item1.Item2; //returns 9
 

 

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.

 

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

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.

 

 

// tuple return type  
         public (string, string, long) TupleReturnLiteral(long id)  
        {  
          
        string name = string.Empty;  
        string title = string.Empty;  
        long year = 0;  
          
        if (id == 1000)  
               {  
               name = MindSpark";  
               title = "ADO.NET Programming";  
               year = 2020;  
        }  
          
        // tuple literal  
        return (name, title, year);  
          
        }  

 

 

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.

 

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

Summary

In this article, we found out about tuples in C# and how to apply the new changes presented in C#