从C#12开始,该语言支持Collection expressions,这允许使用扩散元素合并集合.
我的问题是,有没有some simliar syntax for tuples,特别是合并元组?(见下面的例子)
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'
我知道,我可以这么做:
var tpl3b = (tpl1.Item1, tpl1.Item2, tpl2.Item1, tpl2.Item2);
但对于较大的元组或名称较长的元组来说,这并不方便.