Is there some syntax for merging tuples similar to spread elements?

  Kiến thức lập trình

Since C#12 the language supports Collection expressions, which allows merging collections using a spread element.

My question is, wether or not there is some simliar syntax for tuples, especially to combine tuples? (see example below)

using System;

// Collection expression
int[] arr1 = [1, 2];
int[] arr2 = [3, 4];
int[] arr3 = [..arr1, ..arr2]; // combine using spread element => [1, 2, 3, 4]

// tuples, similar syntax
var tpl1 = (1, 2); 
var tpl2 = (3, 4);

// Sadly this doesn't work? 
var tpl3 = (..tpl1, ..tpl2); // Cannot implicitly convert type '(int, int)' to 'System.Index'

I know, I could do this:

var tpl3b = (tpl1.Item1, tpl1.Item2, tpl2.Item1, tpl2.Item2); 

but it’s unhandy for larger tuples or tuples with longer names.

LEAVE A COMMENT