Shadman Kudchikar

Python Style Tuple In C#

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,

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

Similar code in C#

(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

Tuple<int, string, string> person = new Tuple <int, string, string>(1, "Steve", "Jobs");
person.Item1; // returns 1
person.Item2; // returns "Steve"
person.Item3; // returns "Jobs"

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

Tuples can be used in the following scenarios:

Tuple Limitations:

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,

var person = (1, "Bill", "Gates");
    
//equivalent Tuple
//var person = Tuple.Create(1, "Bill", "Gates");

ValueTuple<int, string,string> person = (1, "Bill", "Gates");
person.Item1;  // returns 1
person.Item2;   // returns "Bill"
person.Item3;   // returns "Gates"


(int, string, string) person = (1, "Bill", "Gates");
person.Item1;  // returns 1
person.Item2;   // returns "Bill"
person.Item3;   // returns "Gates"

(int Id, string FirstName, string LastName) person = ("Bill", "Gates", 60);
person.Id;   // returns 1
person.FirstName;  // returns "Bill"
person.LastName; // returns "Gates"

//We can also assign member names at the right side with values, as below.
var person = (Id:1, FirstName:"Bill", LastName: "Gates");
person.Id;   // returns 1
person.FirstName;  // returns "Bill"
person.LastName; // returns "Gates"


// PersonId, FName, LName will be ignored.
(int Id, string FirstName, string LastName) person = (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.

static void Main(string[] args)
{
    var person = GetPerson();
}

static (int, string, string) GetPerson() 
{
    return (Id:1, FirstName: "Bill", LastName: "Gates");
}

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

(var id, var FName, _) = GetPerson(); // use discard _ for the unused member LName

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

Feel free to ask me questions by sending me a message on my Instagram account, or add me on LinkedIn!

References


comments powered by Disqus