-
-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
Description
The compiler does not allow to have 2 deconstruct methods with the same amount of arguments, so this is not possible, even when declare variable types explicitly:
public static void Deconstruct<T>(this T[] array, out T t1, out T[] rest)
{
}
public static void Deconstruct<T>(this T[] array, out T t1, out T t2)
{
}So, we have 3 options:
- Do not fetch rest of the array
var a = new [] { 1, 2, 3, 4}; var (x,y) = a; //x=1, y=2, 3 & 4 lost in void.
- Fetch rest of the array as an array
If a user does not need the rest of the array he can use
var a = new [] { 1, 2, 3, 4}; var (x,y,z) = a; //x=1,y=2, z=new []{3,4};
_to declare unused variableThe problem is, that we are unable to prevent array slicing from happening.var (x,y,_) = a; //x=1,y=2
- Fetch rest of the array as an IEnumerable
The idea here that rest of the array is an lazy operation which will be delayed until the values are used.
var a = new [] { 1, 2, 3, 4}; var (x,y,z) = a; //x=1,y=1,z=lazy IEnumerable<int> var (x,y,_) = a; //x=1,y=2