C# - How to avoid having Tuple members named item1, item2 itemX etc. in methods, lists and variables

Earlier this year I made a post on how to make a list of named value tuples. I decided to make a new post with some more examples on the syntax of named tuples. If you are like med, you tend to forget what the syntax is and cannot find examples when you need them. The help you get in Rider or Visual Studio is not that great for named tuples, so I decided to make this page and help others.

Note: This is a C# 7 feature, so you will need that for this to work.

Creating a single named tuple with values

The simplest way to make a named tuple and assigning it to a variable is the following:

var tuple = (firstname: "peter",  lastname: "rasmussen");
var firstname = tuple.firstname; //peter
var lastname = tuple.lastname; //rasmussen

In the above we first assign the values peter and rasmussen to the firstname and lastname members of the tuple, we then access these members to get their values. The example uses strings but you can use any type.

Creating a list of named tuples with 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 can contain tuples with two strings named firstname and 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 (syntax sugar) 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

The above is the same as the previous, except that it uses a collection initializer, the result is the same.

Create a method that returns a named value tuple

You may also want to return a named tuple when calling a method:

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 lastname. The method is then invoked and the returned firstname and lastname are accessed.

Prior to C# 7 you would have to create an object that could hold the above values. This often lead to the code base having a lot of wrapper classes for return values or the use of out variables were introduced. The new named tuples bridges this nicely, they are ideal when you need to return a couple of values where the use of a class seems like overkill.

That is it!

This was a short list of examples on how to use the named value tuples in different scenarios. Let me know in the comments down below if you think this should be extended, other feedback is of also welcome.