By using this site, you acknowledge that you have read and understand our Cookie policy, Privacy policy and Terms .
access_time 3 years ago visibility417 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.

Create<T1>(T1)

Create<T1,T2>(T1,T2)

Create<T1,T2,T3>(T1,T2,T3)

Create<T1,T2,T3,T4>(T1,T2,T3,T4)

Create<T1,T2,T3,T4,T5>(T1,T2,T3,T4,T5)

Create<T1,T2,T3,T4,T5,T6>(T1,T2,T3,T4,T5,T6)

Create<T1,T2,T3,T4,T5,T6,T7>(T1,T2,T3,T4,T5,T6,T7)

Create<T1,T2,T3,T4,T5,T6,T7,T8>(T1,T2,T3,T4,T5,T6,T7,T8)

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

Examples

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}");
             Console.ReadLine();
         }
     }
}

The output:

image

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)
         {
             TupleInCSharp7();
             Console.ReadLine();
         }

        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:

image

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: {tuple.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

image

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:

https://blogs.technet.microsoft.com/heyscriptingguy/2014/09/02/using-a-tuple-in-powershell/

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

More from Kontext

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 273
thumb_up 0
access_time 4 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 View open_in_new .NET Machine Learning

local_offer C# local_offer lite-log

visibility 328
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 View open_in_new C# Programming

local_offer teradata local_offer C# local_offer .NET

visibility 6839
thumb_up 0
access_time 5 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 View open_in_new Teradata

local_offer C# local_offer Windows Phone local_offer Linq

visibility 576
thumb_up 0
access_time 9 years ago

Resolve the issue in Linq to SQL in Windows Phone 7 App:
An overflow occurred while converting to datetime.

open_in_new View open_in_new Archived

info About author

Dark theme mode

Dark theme mode is available on Kontext.

Learn more arrow_forward
Kontext Column

Kontext Column

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

Learn more arrow_forward
info Follow us on Twitter to get the latest article updates. Follow us