Is there a way to allow a list of tuples to deconstruct to a List<T>
?
I get the following compile error with the following code sample:
Cannot implicitly convert type 'System.Collections.Generic.List< Deconstruct.Test>' to 'System.Collections.Generic.List<(int, int)>'
using System;
using System.Collections.Generic;
namespace Deconstruct
{
class Test
{
public int A { get; set; } = 0;
public int B { get; set; } = 0;
public void Deconstruct(out int a, out int b)
{
a = this.A;
b = this.B;
}
}
class Program
{
static void Main(string[] args)
{
var test = new Test();
var (a, b) = test;
var testList = new List<Test>();
var tupleList = new List<(int, int)>();
tupleList = testList; // ERROR HERE....
}
}
}
Destructors in C# are methods inside the class used to destroy instances of that class when they are no longer needed.
A Deconstruct method with an out parameter for each positional parameter provided in the record declaration. The method deconstructs properties defined by using positional syntax; it ignores properties that are defined by using standard property syntax.
You need to explicitly convert the testList
(List<Test>
) to tupleList
(List<(int, int)>
)
tupleList = testList.Select(t => (t.A, t.B)).ToList();
Your are using the code as if the Deconstruct
lets you convert a class implementing Deconstruct
to a tuple (ValueTuple
), but that's not what Deconstruct
dose.
From documentation Deconstructing tuples and other types:
Starting with C# 7.0, you can retrieve multiple elements from a tuple or retrieve multiple field, property, and computed values from an object in a single deconstruct operation. When you deconstruct a tuple, you assign its elements to individual variables. When you deconstruct an object, you assign selected values to individual variables.
Deconstruction returns multiple elements to individual variables, not a tuple (ValueTuple
).
Attempting to convert a List<Test>
to a List<(int, int)>
like this:
var testList = new List<Test>();
var tupleList = new List<(int, int)>();
tupleList = testList;
cannot work because you cannot convert a List<Test>
to a List<(int, int)>
. It will generate a compiler error:
Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List<(int, int)>'
Attempting to cast each Test
element to a (int, int)
like this:
tupleList = testList.Cast<(int, int)>().ToList();
cannot work because you cannot cast a Test
to a (int, int)
. It will generate a run-time error:
System.InvalidCastException: 'Specified cast is not valid.'
Attempting to convert a single Test
element to a (int, int)
like this:
(int, int) tuple = test;
cannot work because you cannot convert a Test
to a (int, int)
. It will generates a compiler error:
Cannot implicitly convert type 'Deconstruct.Test' to '(int, int)'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With