C# - How to make a list of tuples with named values

I found it hard to find an example online showing a simplistic way to create named value tuples in a list. Most of the examples I could find were using a very verbose syntax, therefore I decided to write this post. As a heads up this syntax requires C# 7.

Creating named value tuples in a list

Here is a very minimalistic way to create a list of tuples with two string values:

var tupleList = new List<(string Firstname, string Lastname)> 
    ( "Peter", "Rasmussen" ),
    ( "John", "Doe" )

var peterFirstname = tupleList[0].Firstname;
var peterLastname = tupleList[0].Lastname;

var johnFirstname = tupleList[1].Firstname;
var johnLastname = tupleList[1].Lastname;

The above is syntactic sugar, it uses the new named tuples from C# 7 combined with an object initialiser. In few lines you have yourself a new list of simple values. In the above we first create the list and then access each value one after another in order to show that you can use the names given for the fields.

Selecting a class as a named tuple (LINQ query projection)

Another scenario is that you want to select a list of classes as a list of named tuples. If we have the following class:

class Person
    public string Firstname { get; set; }
    public string Lastname { get; set; }

and we create a list of objects of that class:
var list = new List<Person>
    new Person { Firstname = "Peter", Lastname = "Rasmussen" },
    new Person { Firstname = "John", Lastname = "Doe" },

We can then use select to get the list of classes as a list of named tuples:
var tupleListFromClass = list.Select(x => new { 
    Firstname = x.Firstname,
    Lastname = x.Lastname

In the above we use the same names for the tuple named values and the class properties, that might be confusing and seem redundant - but it is there to complete the example in case they are not the same for you.

Prior to C# 7

Prior to C# 7 you would have to write it like the following:

var tupleList = new List<Tuple<string, string>>
    new Tuple<string, string>("Peter", "Rasmussen" ),
    new Tuple<string, string>("John", "Doe" ),

Using the above your tuples would not be named and you would have to access them using Item1, Item2, etc. Getting the first value in the first tuple would look like the following: var peterFirstname = tupleList[0].Item1;. The new syntax for tuples is much more readable, simplistic and much less verbose.

Differences between value tuple and tuples

Value tuples are not just all syntactic sugar, Some of the differences are:

  • Value tuples are value types (structs) and tuples are reference types (classes)
  • Value tuples uses fields rather than properties
  • Value tuple is mutable and tuples are immutable

In most use cases the above matters little, but it is great to keep in mind!

That is all

I hope this was the example of creating tuples in a list you were looking for! If you know an even better way, please let me know in the comments down below! :)