Working with Tuple in C#
Tags: csharp, dotnet, tipsandtricks
Published: 2023-04-03 13:07:26

---
title: Working with Tuple in C#
published: true
date: 2023-04-03 11:00:00 UTC
tags: csharp, dotnet, tipsandtricks
canonical_url: https://www.syncfusion.com/blogs/post/working-with-tuple-in-csharp.aspx
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/565rjztjrg1efcd0hqs3.png
---
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.

## Basic example for a tuple in C#

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

```csharp
(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.

```csharp
(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.

```csharp
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.

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

We can also access the item inside the nested tuple.

```csharp
Console.WriteLine(employees.Item1.item1); 
//23
```
## 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.

```csharp
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.

```csharp
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.

```csharp
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.

```csharp
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
```
## Tuple deconstruction in C#

Let’s look at this example.

```csharp
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.

```csharp
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.

```csharp
(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](https://www.syncfusion.com/winforms-ui-controls "WinForms UI controls"), [WPF](https://www.syncfusion.com/wpf-controls "WPF UI controls"), [.NET MAUI](https://www.syncfusion.com/maui-controls ".NET MAUI controls"), ASP.NET ([Web Forms](https://www.syncfusion.com/jquery/aspnet-web-forms-ui-controls "ASP.NET Web Forms UI components"), [MVC](https://www.syncfusion.com/aspnet-mvc-ui-controls "ASP.NET MVC UI components"), [Core](https://www.syncfusion.com/aspnet-core-ui-controls "ASP.NET Core UI components")), [UWP](https://www.syncfusion.com/uwp-ui-controls "UWP UI controls"), [WinUI](https://www.syncfusion.com/winui-controls "WinUI controls"), [Xamarin](https://www.syncfusion.com/xamarin-ui-controls "Xamarin UI controls"), [Flutter](https://www.syncfusion.com/flutter-widgets "Flutter widgets"), [Blazor](https://www.syncfusion.com/blazor-components "Blazor UI components"), [JavaScript](https://www.syncfusion.com/javascript-ui-controls "JavaScript UI components"), [Angular](https://www.syncfusion.com/angular-ui-components "Angular UI components"), [Vue](https://www.syncfusion.com/vue-ui-components "Vue UI components"), and [React](https://www.syncfusion.com/react-ui-components "React UI components") that make developers’ work easier.

Please share your feedback as comments on this blog. You can also reach us through our [support forums](https://www.syncfusion.com/forums "Syncfusion Support Forums"), [support portal](https://support.syncfusion.com/ "Syncfusion Support Portal"), or [feedback portal](https://www.syncfusion.com/feedback/ "Syncfusion Feedback Portal").

## Related blogs

- [Syncfusion Essential Studio 2023 Volume 1 Is Here!](https://www.syncfusion.com/blogs/post/essential-studio-2023-volume-1.aspx "Blog: Syncfusion Essential Studio 2023 Volume 1 Is Here!")
- [Easiest Ways to Highlight Data in Excel Using C#](https://www.syncfusion.com/blogs/post/highlight-data-in-excel-using-csharp.aspx "Blog: Easiest Ways to Highlight Data in Excel Using C#")
- [3 Easy Ways to Convert Word Documents to Images in C#](https://www.syncfusion.com/blogs/post/convert-word-documents-to-images-in-csharp.aspx "Blog: 3 Easy Ways to Convert Word Documents to Images in C#")
- [Generate Dynamic PDF Reports from an HTML Template Using C#](https://www.syncfusion.com/blogs/post/generate-dynamic-pdf-reports-from-html-template-using-csharp.aspx "Blog: Generate Dynamic PDF Reports from an HTML Template Using C#")