Get Started with Tuples in C#: A Beginner's Handbook | Syncfusion Blogs
Detailed Blog page Skeleton loader
Working with Tuple in C#

We create tons of classes to create objects for many scenarios during software development. But sometimes, we don’t need to create a new class for an object with few properties. To handle these cases, C# offers a feature named tuples.

This article will discuss tuples in C# with some practical use cases for better understanding.

What is a tuple in C#?

As developers, we have used many data structures, such as arrays, queues, and linked lists, to categorize and handle data. A tuple is a simple and lightweight data structure consisting of multiple elements to represent data. It is available in C# 7.0 and higher versions.

Tuples help us access and manipulate data sets easily for different operations without having to define a new class with custom properties. First, let’s explore tuples with some examples.

Easily build cross-platform mobile and desktop apps with the flexible and feature-rich controls of the Syncfusion .NET MAUI platform.

Basic example for a tuple in C#

Here, we’ll show how to create a tuple with a basic example in C#.

(int, string) employee1 = (23, "Yohan");
Console.WriteLine($"{employee1.Item2} is {employee1.Item1} years old");
//Yohan is 23 years old

In this example, the requirement is to create a simple employee object to access an employee’s name and age. For this case, creating a tuple would be enough, rather than creating a class. We have created properties of type int and string in this tuple. When you haven’t provided names to the properties, you can access the properties using the syntax employee1.item1.

Tuples with fields names in C#

We didn’t use field names in the previous example. But we can provide names to the properties. Refer to the following code example.

(int Age, string Name) employee1 = (23, "Yohan");
Console.WriteLine($"{employee1.Name} is {employee1.Age} years old"); 
//Yohan is 23 years old

We used field names for the data in the employee1 tuple. Then, we could access the data using field names. The code is more readable when using the field names.

Nested tuple in C#

You can also create multiple tuples inside one tuple as nested tuples. To create nested tuples, refer to the following example.

var employees = ((23, "Yohan"), (45, "Jhon Doe"), (34, "Jon Jones"));

We have declared a nested tuple with three tuples that use int and string properties. We can now access these items one by one as needed.

To access the first nested tuple in the employee tuple, see the following code.

Console.WriteLine(employees.Item1); 
//(23, "Yohan")

We can also access the item inside the nested tuple.

Console.WriteLine(employees.Item1.item1); 
//23

Every property of the Syncfusion .NET MAUI controls is completely documented to make it easy to get started.

Tuple as a function parameter in C#

We can also use tuples as parameters for functions. Let’s create a function using a tuple as a parameter, as in the following code.

public void GetEmployee((string Name, int Age) employee)
{
    Console.WriteLine($"{employee.Name} is {employee.Age} years old");
}

Then, call this function inside the Main method, as in the following code example.

public class TupleDemo
{
    public void GetEmployee((string Name, int Age) employee)
    {
        Console.WriteLine($"{employee.Name} is {employee.Age} years old");
    }
    public static void Main(string[] args)
    {
        var tupleDemo = new TupleDemo();
        tupleDemo.GetEmployee(("Yohan", 23));
    }
}
//Yohan is 23 years old

In this code, we created the class object for TupleDemo and then called the GetEmployee function using that object. Also, we passed a tuple as the parameter for this function. In this case, we have created the tuple as the parameter for the GetEmployee method.

Tuple as the return type in C#

We can use a tuple as the return type for functions. But for that, we have to use tuple for the return type, as in the following function.

public (string, int) GetEmployee()
{
    return ("Yohan", 23);
}

In it, we created the function named GetEmployee with a tuple as the return type. Here, we have returned the tuple, which contains string and int data properties.

Refer to the following code to call the function GetEmployee and access the values of the tuple.

public class TupleDemo
{
    public static void Main(string[] args)
    {
        var tupleDemo = new TupleDemo();
        var employee = tupleDemo.GetEmployee();
        Console.WriteLine($"{employee.Item1} is {employee.Item2} years old");
    }
    
    public (string, int) GetEmployee()
    {
        return ("Yohan", 23);
    }
}
//Yohan is 23 years old

To make it easy for developers to include Syncfusion .NET MAUI controls in their projects, we have shared some working ones.

Tuple deconstruction in C#

Let’s look at this example.

public class TupleDemo
{
    public static void Main(string[] args)
    {
        var tupleDemo = new TupleDemo();
        var employee = tupleDemo.GetEmployee();
        Console.WriteLine($"{employee.Item1} is {employee.Item2} years old");
    }
    
    public (string, int) GetEmployee()
    {
        return ("Yohan", 23);
    }
}
//Yohan is 23 years old

In this example, we created the function named GetEmployee with tuple as the return type. Then, we called that function by creating the class object and accessing the data of the tuple. We have access to the data like this employee.Item1. But, it is less readable. We can use tuple deconstruction to rescue it.

public class TupleDemo
{
    public void GetEmployee((string Name, int Age) employee)
    {
        Console.WriteLine($"{employee.Name} is {employee.Age} years old");
    }
    public static void Main(string[] args)
    {
        var tupleDemo = new TupleDemo();
        (string Name, int Age) = tupleDemo.GetEmployee();
        Console.WriteLine($"{Name} is {Age} years old");
    }
    
    public (string, int) GetEmployee()
    {
        return ("Yohan", 23);
    }
}
//Yohan is 23 years old

In this example, we have used tuple deconstruction to unpackage all items in a tuple with a single-line operation.

(string Name, int Age) = tupleDemo.GetEmployee();

We can increase the readability of the code by using tuple deconstruction when using tuples.

Conclusion

This article demonstrated the usage of tuples in C# programming with some useful scenarios and code examples.

The tuple is a good programming concept that will help to reduce the need to create classes and structs when we don’t need them. We can also use them to pass multiple values to the functions and use them as the return type.

I hope this article was helpful in learning about tuples in C#.

Thank you for reading!

Syncfusion’s Essential Studio is a software package that provides state-of-the-art solutions for startups and enterprises. It includes more than 1,700 components and frameworks for WinForms, WPF, .NET MAUI, ASP.NET (Web Forms, MVC, Core), UWP, WinUI, Xamarin, Flutter, Blazor, JavaScript, Angular, Vue, and React that make developers’ work easier.

Please share your feedback as comments on this blog. You can also reach us through our support forums, support portal, or feedback portal.

Related blogs

Be the first to get updates

A. Yohan Malshika

Meet the Author

A. Yohan Malshika

I am a Software Engineer who loves to explore technologies and familiar with ASP.NET Core, ASP.NET MVC, Angular, Azure, Flutter, and Blazor. I also love to write tech blogs and share knowledge with others.