There are two reasons you are on this page: 1) you forgot the syntax for named tuples 2) You have just learned about named tuples and want to get started. If you are in category 1 start scrolling down for those code snippets else keep reading! Prior to version 7 of C# there were no named tuples and you had to write your tuples in the following way:
var tuple = new Tuple<string, string>("peter", "rasmussen");
var firstname = tuple.Item1; //peter
var lastname = tuple.Item2; //rasmussen
In the above we create a new tuple with two strings and give the tuple the values "peter" and "rasmussen". As you can see the elements of it are named Item1
and Item2
which makes the code hard to read as it can be hard to remember what is Item1
and what is Item2
. The more elements your tuple has, the harder it is to remember what Item1
, Item2
and ItemX
really mean. This is where named tuples come in.
Using named tuples
Instead of having to use Item1
, Item2
and ItemX
you can give the element a meaningful name by naming your tuple elements like below:
var tuple = (firstname: "peter", lastname: "rasmussen");
var firstname = tuple.firstname; //peter
var lastname = tuple.lastname; //rasmussen
In the above we can use .firstname
and .lastname
instead of .Item1
and .Item2
- this makes the code more readable. It works just the same as not naming the tuples and it is all just syntax sugar.
Creating a list of tuples with named values
You can easily make a list of named tuples using the following syntax:
var tupleList = new List<(string firstname, string lastname)>();
tupleList.Add(("peter", "rasmussen"));
var firstname = tupleList[0].firstname; //peter
var lastname = tupleList[0].lastname; //rasmussen
In the above we make a list that has two string elements, one named firstname and one named lastname. We then add a tuple with my name and lastname to the list and access this item (the only item in the list).
You can also use a collection initializer to assign values as you create the list:
var tupleList = new List<(string firstname, string lastname)> {
("peter", "rasmussen")
};
var firstname = tupleList[0].firstname; //peter
var lastname = tupleList[0].lastname; //rasmussen
This gives the same result as the previous example (also at compile time), but depending on your preference you may like it more! I wrote a specific post on the syntax of a list of named tuples here.
Create a method that returns a named value tuple
Below is the syntax for returning a named value tuple:
public (string firstname, string lastname) Get()
{
return ("peter", "rasmussen");
}
//calling the method:
var tuple = Get();
var firstname = tuple.firstname; //peter
var lastname = tuple.lastname; //rasmussen
In the above example the method returns a tuple with a firstname and a last name. The method is then invoked and the returned firstname and lastname are accessed. It uses the same example as before in this post.
Prior to having named tuples you would have to create a new class that could hold the above values if you wanted the easily read names. This often leads to the code base having a lot of wrapper classes for return values or the use of out variables. The named tuples are perfect for this, as they are ideal when you need to return a couple of values where the use of a class seems like too much trouble.
I also wrote a post on how to do this with a method that returns a task that has a tuple as return type here.
Wrapping it up
If the above is not working you are likely not having C# version 7, as this is required for having named tuples. I hope you found this helpful, please leave a comment down below, I read all of them!