Python Style Tuple In C#

Contents

What Is Tuple In Python?

A Tuple is a collection of Python objects separated by commas. In someways a tuple is similar to a list in terms of indexing, nested objects and repetition but a tuple is immutable unlike lists which are mutable.

Lets see some code example in python,

1 firstName, lastName = ("Bill", "Gates")

Similar code in C#

1 (string FirstName, string LastName) person = ("Bill", "Gates");

Lets see how C# went from close to actual python style tuple in C#,

Far From Python Style Tuple

The Tuple class was introduced in .NET Framework 4.0. A tuple is a data structure that contains a sequence of elements of different data types. It can be used where you want to have a data structure to hold an object with properties, but you don't want to create a separate type for it.

Lets see some sample code

 1 Tuple<int, string, string> person = 
 2 			new Tuple <int, string, string>(1, "Steve", "Jobs");
 3 person.Item1; // returns 1
 4 person.Item2; // returns "Steve"
 5 person.Item3; // returns "Jobs"
 6 
 7 var person = Tuple.Create(1, "Steve", "Jobs");
 8 person.Item1; // returns 1
 9 person.Item2; // returns "Steve"
10 person.Item3; // returns "Jobs"
11 
12 
13 
14 var numbers = Tuple.Create("One", 2, 3, "Four", 5, "Six", 7, 8);
15 numbers.Item1; // returns "One"
16 numbers.Item2; // returns 2
17 numbers.Item3; // returns 3
18 numbers.Item4; // returns "Four"
19 numbers.Item5; // returns 5
20 numbers.Item6; // returns "Six"
21 numbers.Item7; // returns 7
22 numbers.Rest; // returns (8)
23 numbers.Rest.Item1; // returns 8

Tuples can be used in the following scenarios:

  • When you want to return multiple values from a method without using ref or out parameters.
  • When you want to pass multiple values to a method through a single parameter.
  • When you want to hold a database record or some values temporarily without creating a separate class.

Tuple Limitations:

  • Tuple is a reference type and not a value type. It allocates on heap and could result in CPU intensive operations.
  • Tuple is limited to include 8 elements. You need to use nested tuples if you need to store more elements. However, this may result in ambiguity.
  • Tuple elements can be accessed using properties with a name pattern Item which does not make sense.

C# 7 includes ValueTuple to overcome the limitations of Tuple and also makes it even easier to work with Tuple. Lets learn more about it,

Python Style Tuple In C# 7.0

C# 7.0 (.NET Framework 4.7) introduced ValueTuple, a structure which is a value type representation of the Tuple.

The ValueTuple is only available in .NET Framework 4.7. If you don’t see ValueTuple in your project then you need to install the ValueTuple. (.NET Framework 4.7 or higher, or .NET Standard Library 2.0 or higher already includes ValueTuple.)

To install the ValueTuple package, right click on the project in the solution explorer and select Manage NuGet Packages… This will open the NuGet Package Manager. Click the Browse tab, search for ValueTuple in the search box and select the System.ValueTuple package.

Lets see some code example,

 1 var person = (1, "Bill", "Gates");
 2     
 3 //equivalent Tuple
 4 //var person = Tuple.Create(1, "Bill", "Gates");
 5 
 6 ValueTuple<int, string,string> person = (1, "Bill", "Gates");
 7 person.Item1;  // returns 1
 8 person.Item2;   // returns "Bill"
 9 person.Item3;   // returns "Gates"
10 
11 
12 (int, string, string) person = (1, "Bill", "Gates");
13 person.Item1;  // returns 1
14 person.Item2;   // returns "Bill"
15 person.Item3;   // returns "Gates"
16 
17 (int Id, string FirstName, string LastName) person = ("Bill", "Gates", 60);
18 person.Id;   // returns 1
19 person.FirstName;  // returns "Bill"
20 person.LastName; // returns "Gates"
21 
22 //We can also assign member names at the right side with values, as below.
23 var person = (Id:1, FirstName:"Bill", LastName: "Gates");
24 person.Id;   // returns 1
25 person.FirstName;  // returns "Bill"
26 person.LastName; // returns "Gates"
27 
28 
29 // PersonId, FName, LName will be ignored.
30 (int Id, string FirstName, string LastName) person = 
31 				(PersonId:1, FName:"Bill", LName: "Gates");

You may have notice now from above example how this C# tuples are now much alike to python style tuples. We can also specify different member names for a ValueTuple returned from a method.

 1 static void Main(string[] args)
 2 {
 3     var person = GetPerson();
 4     (var id, var fname, var lname) = GetPerson();
 5 }
 6 
 7 static (int Id, string FirstName, string LastName) GetPerson()
 8 {
 9     return (1, "Bill", "Gates");
10 }

ValueTuple also allows “discards” in deconstruction for the members you are not going to use.

1 (var id, var fname, _) = GetPerson(); // use discard _ for the unused member LastName

You can learn more about ValueType Tuples in C# here.

References

Python Style Tuple In C#
Share this

Subscribe to Code with Shadman