tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

Tuples goodness in .NET 4.7.1 (.NET Core 2.0 included)

24 Jan 2018 1 mins .NET, .NET Core, .NET Standard, C#

One of the few things I was missing when tuples were introduced was some way to generically work with unknown tuples. Mostly to be able to identify tuples, instead of using plain object and also work with items using index of some sort. Luckily, I was probably not the only one and starting .NET 4.7.1 new interesting interface – ITuple – was added (also available in .NET Core 2.0).

This interface, first and foremost, allows you to accept any tuple without using object as a type and doing some reflection magic. Every tuple implements this interface (explicitly) so you don’t have to do anything to use it.

Second nice thing, my favorite, is the indexer Item[Int32]. With it you can easily access tuple elements, again without reflection or other magic.

And finally, you can get the size or tuple using Length property. Again, saves a lot of reflection code.

Putting it all together in a nice, clean, readable code.

static void Main(string[] args)
	var data = (10, "ten", 4.6, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);

static void FooBar(ITuple tuple)
	Console.WriteLine($"# of elements: {tuple.Length}");
	Console.WriteLine($"Second to last element: {tuple[tuple.Length - 2]}");

Sadly, at the moment, the ITuple is not part of .NET Standard (2.0), yet.

Profile Picture Jiří Činčura is .NET, C# and Firebird expert. He focuses on data and business layers, language constructs, parallelism, databases and performance. For almost two decades he contributes to open-source, i.e. FirebirdClient. He works as a senior software engineer for Microsoft. Frequent speaker and blogger at