Having fun with ValueTuple

12/2/2024
3 minute read

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 type is a nice syntactic sugar. So if you have the following code, even though you named the tuple elements:

(var one, var two) = MyFunc();
Console.WriteLine(one + two);

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

This will be translated by the compile to:

[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);
    }
}

Source: sharplab.io

So the compiler generates a list of Items numbered from 1 to however many elements you have. But there is something interesting happening, if you add more than 7 items:

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:

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);

Source: Sharplab.io

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:

(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:

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

How to enumerate through a StringBuilder

Did you ever wonder how we can iterate through a StringBuilder? I mean, of course, we can just call ToString and use the returned string, but that means we materialize the whole thing without good reason.

We can also use a normal for-loop. But we can also find a completely different and probably dumber way! And if you wonder: No, this is not something you do in your daily life, but by doing so, I can show some cool stuff C# and .NET offer.

Mutable value types are evil! Sort of...

You might have heard that mutable value types are evil. But why is that and why does the .NET framework use them then? Are they really that evil?

Let's have a look at a few examples and have a look what is going on!

Some cool things you can do with ValueTuple

In this blog post, we will explore some cool things you can do with ValueTuple. Also, a short explanation of what ValueTuple is and how it works.

An error has occurred. This application may no longer respond until reloaded. Reload x