access_time 3 years ago visibility438 comment 0 languageEnglish

What is a tuple?

Tuple is an ordered sequence, fixed-size, immutable and of heterogeneous objects. Tuple has been existing in other languages such as F#, Python, Perl and etc. for a long time. It was first introduced into C# from C# 4.0 and has been evolving over time. Since C# 7.1, tuple has become very easy to use compared with previous version.

Create a tuple is more efficient and more productive:

  • Save the work of authoring a struct or class that defining the types returned.
  • No need to create a new type.

Tuple in .NET 4.x

Tuple class

Tuple class has 8 methods to create tuples from 1-tuple (singleton) to 8-tuple (octuple). Tuple inherits from object.









In the exetension methods, you can also construct and deconstruct tuples up to a 18-tuple.


The following example instantiated a tuple through two

using System;

namespace TestConsoleApp
     class Program
         static void Main(string[] args)
             // Instantiate tuple through constructor
             var tupleExample1 = new Tuple<int, string, bool>(1, "Tuple example 1", true);
             Console.WriteLine($"Tuple#:{tupleExample1.Item1}\r\nTuple Name: {tupleExample1.Item2}\r\nBool Value: {tupleExample1.Item3}");

            // Instantiate tuple through Create method
             var tupleExample2 = Tuple.Create(2, "Tuple example 2", false);
             Console.WriteLine($"Tuple#:{tupleExample2.Item1}\r\nTuple Name: {tupleExample2.Item2}\r\nBool Value: {tupleExample2.Item3}");

            // Deconstruct a tuple into variables
             tupleExample1.Deconstruct(out int item1, out string item2, out bool item3);
             Console.WriteLine($"Tuple#:{item1}\r\nTuple Name: {item2}\r\nBool Value: {item3}");

The output:


Tuple in C# 7.x

In the previous version, tuple is supported via a class without language support. The new tuples features require the ValueTuple types. You must add the NuGet package System.ValueTuple in order to use it on platforms that do not include the types. This is due to the different ship cadence for .NET framework and language compilers.

Easy syntax

using System;

namespace TestConsoleApp
     class Program
         static void Main(string[] args)

        static void TupleInCSharp7()
             // Creat a tuple by direct assignment
             var tupleExample1 = (1, "Tuple example 1", true);
             Console.WriteLine($"Tuple#:{tupleExample1.Item1}\r\nTuple Name: {tupleExample1.Item2}\r\nBool Value: {tupleExample1.Item3}");

            // Create a tuple that provide semantic names
             (int Num, string Name, bool Flag) tupleExample2 = (2, "Tuple example 2", false);
             Console.WriteLine($"Tuple#:{tupleExample2.Num}\r\nTuple Name: {tupleExample2.Name}\r\nBool Value: {tupleExample2.Flag}");

            // Create a tuple that provides semantic names, the right side names will be ignored.
             (int Num, string Name, bool Flag) tupleExample3 = (num: 2, name: "Tuple example 2", flag: false);
             Console.WriteLine($"Tuple#:{tupleExample3.Num}\r\nTuple Name: {tupleExample3.Name}\r\nBool Value: {tupleExample3.Flag}");

            // Deconstruct
             (int num, string name, bool flag) = tupleExample3;
             Console.WriteLine($"Tuple#:{num}\r\nTuple Name: {name}\r\nBool Value: {flag}");

            // Discards num and flag if only interested in name
             (_, string name2, _) = tupleExample3;
             Console.WriteLine($"Tuple#:-\r\nTuple Name: {name2}\r\nBool Value: -");

Output for the above:


Inferred tuple element names

C# 7.1 further enhanced tuple to enable inferred element names in tuple. For example:

var num = 1;
var name = "Tuple in C# 7.1";
var flag = true;
var tuple = (num, name, flag);
Console.WriteLine($"Tuple#:{tuple.num}\r\nTuple Name: {}\r\nBool Value: {tuple.flag}");

You will encounter compiler error if your IDE

Error    CS8306    Tuple element name 'flag' is inferred. Please use language version 7.1 or greater to access an element by its inferred name.    TestConsoleApp    F:\Projects\TestProject\TestConsoleApp\Program.cs    15    Active

You can change the build settings if this error happens. The default setting is the major version instead of minor version.

Project->Properties->Build->Advanced->Language Version->C# latest Minor Version


Tuple in PowerShell

You can definitely use tuple in PowerShell since you can do it in C#.

Refer to the following blogs for some examples:

info Last modified by Raymond at 3 years ago * This page is subject to Site terms.

More from Kontext

Entity Framework Core Code-First - Generate Covering Index with Columns Included

local_offer entity-framework local_offer core local_offer Azure local_offer C#

visibility 17
thumb_up 0
access_time 7 days ago

In SQL Server or some other relational databases, it is a very common requirement to create covering index with columns included in index pages beside the index key columns. With Entity Framework Core, you can also easily generate covering indexes using purely C# code. Scenario For ...

open_in_new ASP.NET Core

Machine Learning with .NET in Jupyter Notebooks

local_offer plot local_offer machine-learning local_offer jupyter-notebook local_offer C# local_offer dotnet core

visibility 473
thumb_up 0
access_time 7 months ago

In this article, I'm going to show you how to install Jupyter in Windows and then install .NET kernel for Jupyter notebooks. It also shows a machine learning example using ML.NET. The target audience are .NET developers who want to expand their skills in data engineering and science domain...

open_in_new .NET Machine Learning

local_offer C#

visibility 361
thumb_up 0
access_time 3 years ago

C# is my favourite programming language and has been evolving overtime. I have been programming with many other languages but C# is always my favourite because it is simple and easy to use. Visual Studio is the most powerful tool I’ve ever used. To me, programming efficiency and peformance are th...

open_in_new C# Programming

local_offer teradata local_offer C# local_offer .NET

visibility 7369
thumb_up 0
access_time 6 years ago

In this post, I will demonstrate how to connect to Teradata database via .NET Data Provider for Teradata using C#. Prerequisites Install the .NET Data Provider for Teradata from the following link: ...

open_in_new Teradata

info About author

comment Comments (0)

comment Add comment

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

No comments yet.

Dark theme mode

Dark theme mode is available on Kontext.

Learn more arrow_forward

Kontext Column

Created for everyone to publish data, programming and cloud related articles. Follow three steps to create your columns.

Learn more arrow_forward