Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deconstruct with List<T>

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....
        }
    }
}
like image 252
rhughes Avatar asked Jun 23 '19 11:06

rhughes


People also ask

Can you Destructure in C#?

Destructors in C# are methods inside the class used to destroy instances of that class when they are no longer needed.

What does the deconstruct method do for a record C#?

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.


1 Answers

You need to explicitly convert the testList (List<Test>) to tupleList (List<(int, int)>)

tupleList = testList.Select(t => (t.A, t.B)).ToList();

Explanation:

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)'

like image 172
Eliahu Aaron Avatar answered Sep 20 '22 09:09

Eliahu Aaron