---
title: "Having fun with ValueTuple"
description: "Just as a heads up: This blog post will probably bring you 0 value in your daily life - well, maybe a short smile. Let's put `ValueTuple` to its extreme!"
updated: 2024-12-02
tags: ["C#", ".NET"]
canonical: "https://steven-giesel.com/blogPost/0a3db64c-e514-45fc-a1a1-2b79cc42f797/having-fun-with-valuetuple"
---

# Having fun with ValueTuple

Just as a heads up: This blog post will probably bring you 0 value in your daily life - well, maybe a short smile. Let's put `ValueTuple` to its extreme!

## `ValueTuple`

The [`ValueTuple`](https://learn.microsoft.com/en-us/dotnet/api/system.valuetuple?view=net-8.0) type is a nice [syntactic sugar](https://steven-giesel.com/blogPost/69dc05d1-9c8a-4002-9d0a-faf4d2375bce/c-lowering). So if you have the following code, even though you named the tuple elements:
```csharp
(var one, var two) = MyFunc();
Console.WriteLine(one + two);

(int ANumber, int AnotherNumber) MyFunc() => (1,2);
```

This will be translated by the compile to:
```csharp
[CompilerGenerated]
internal class Program
{
    private static void <Main>$(string[] args)
    {
        ValueTuple<int, int> valueTuple = <<Main>$>g__MyFunc|0_0();
        int item = valueTuple.Item1;
        int item2 = valueTuple.Item2;
        Console.WriteLine(item + item2);
    }

    [CompilerGenerated]
    [return: TupleElementNames(new string[] { "ANumber", "AnotherNumber" })]
    internal static ValueTuple<int, int> <<Main>$>g__MyFunc|0_0()
    {
        return new ValueTuple<int, int>(1, 2);
    }
}
```
<sup>Source: [sharplab.io](https://sharplab.io/#v2:D4AQTAjAsAULAUA3AhgJwAQHsB2BTANOihgC4DumAlOgLzoCyAniAKzyUDcsIEAnPDlzoA1OnJUucGPACW2EugCCAOQCuAWwBGuVITkLF2TCQAWOtVp3UmrdrQB86eBHxhOQA===)</sup>

So the compiler generates a list of `Item`s numbered from 1 to however many elements you have. But there is something interesting happening, if you add more than **7** items:
```csharp
var (a,b,c,d,e,f,g, h) = MyFunc();
Console.WriteLine(a + b + c + d + e + f + g + h);

(int a, int b, int c, int d, int e, int f,int g, int h) MyFunc() 
    => (1,2,3,4,5,6,7,8);
```

The compiler will lower this to:
```csharp
ValueTuple<int, int, int, int, int, int, int, ValueTuple<int>> valueTuple = <<Main>$>g__MyFunc|0_0();
int item = valueTuple.Item1;
int item2 = valueTuple.Item2;
int item3 = valueTuple.Item3;
int item4 = valueTuple.Item4;
int item5 = valueTuple.Item5;
int item6 = valueTuple.Item6;
int item7 = valueTuple.Item7;
int item8 = valueTuple.Rest.Item1;
Console.WriteLine(item + item2 + item3 + item4 + item5 + item6 + item7 + item8);
```
<sup>Source: [Sharplab.io](https://sharplab.io/#v2:D4AQTAjAsAULBuBDATgAgBSIDQCMsGMsATLAUywDMsBzLVACwEpUBeVAWQE8QBWdRgNywQEAJyZUAalQ4pqfHKJzScinOpymQuDHQBLAHYAXVNlSGTec8fl0LqEtZPknqKvdqumHbn2axUQNYAPgwILDAsAGYsABYsHiwANiwAdiwADkEgA=)</sup>

After the 7th item, the compiler generates a new `Rest` property handling, well, the rest! So if you overdo the trick a bit, like this:
```csharp
(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, 
 int k, int l, int m, int n, int o, int p, int q, int r, int s, int t,
 int u, int v, int w, int x, int y, int z, int aa, int bb, int cc, int dd,
 int ee, int ff, int gg, int hh, int ii, int jj, int kk, int ll, int mm, int nn,
 int oo, int pp, int qq, int rr, int ss, int tt, int uu, int vv, int ww, int xx) MyFunc()
{
    return (1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
            11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
            21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
            31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
            41, 42, 43, 44, 45, 46, 47, 48, 49, 50);
}
```

And use it like this:
```csharp
var (a,b,c,....xx) = MyFunc();
```

Then you get something like `valueTuple.Rest.Rest.Rest.Rest.Item1`. If you want to play around: [sharblap.io](https://sharplab.io/#v2:D4AQTAjAsAULBuBDATgAmQUwM4FcA2ALqgLyoCyAniAKwAUAlANyywgQCctAJAEQDKOALYAuVAG9MuQgDpEqANTps+AtIBGCpVNUBjTZJXSAJvuUyMp7dIBmlwwHM7MgBZPVASzfSAVptioArUMAay88L0EvADsvAHsvAAcvAEcvNEUDGSwvIkV/QMzVHC94LwB3LwAPLwovAC8vRDkMs1U1DRarHT1OwyMTPJhAoPMLXplrW3HVe0dp6WdXefdPee9feeDQ+bxw+cFI+aiYweHC6Vj4+YSk+eTU+eR0kdUsbPmCXJfpHGL5+FK8zKFXmlUqAF8eEwWDBaO4okREAAaVDwohqFFo1A6TEI1BGXFEDCE1DWEn2EnOEnuEneFH+LHBEl4EmCElREmxEkJEnJEnIElYEkEJEMvE4EnwEllEmVEkUEl1ElNEntEndEn9UVDLEYYmovGTckUg1ERbUmmm1DrElbZksq0HdkcsVES7cnlW+78gVWt7CkVW36SqVW4Gyyr0chUOj0WBifIBEAAdlQtAgKLAKIAzCiACwo6gogBsKOTKIAHCj2CiIAAGbXDJuBCAZ1AQLPt3Ptgvtovt0vt8vtqvtmuoMANxPNwKQTOdsDdsC9sD9sCDsDDsCjsDj7NToYz4bZtvZzvZ7vZ3vZ/vZwfZ4fZ0fZ8d5g9H4Z5tt5zt57t53s837PNBzzYc81HPNx2oOtoRgcEgA===)