用列表解构<T>
Deconstruct with List<T>
有没有办法让元组列表解构为 List<T>
?
我在使用以下代码示例时遇到以下编译错误:
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....
}
}
}
您需要明确地将 testList
(List<Test>
) 转换为 tupleList
(List<(int, int)>
)
tupleList = testList.Select(t => (t.A, t.B)).ToList();
解释:
您正在使用代码,就好像 Deconstruct
允许您将实现 Deconstruct
的 class 转换为元组 (ValueTuple
),但这不是 Deconstruct
剂量。
来自文档 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.
解构returns多个元素为单个变量,不是元组(ValueTuple
).
正在尝试将 List<Test>
转换为 List<(int, int)>
,如下所示:
var testList = new List<Test>();
var tupleList = new List<(int, int)>();
tupleList = testList;
无法工作,因为您无法将 List<Test>
转换为 List<(int, int)>
。它将产生一个编译器错误:
Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List<(int, int)>'
正在尝试将每个 Test
元素转换为 (int, int)
,如下所示:
tupleList = testList.Cast<(int, int)>().ToList();
无法工作,因为您无法将 Test
转换为 (int, int)
。它将生成 run-time 错误:
System.InvalidCastException: 'Specified cast is not valid.'
正在尝试将单个 Test
元素转换为 (int, int)
,如下所示:
(int, int) tuple = test;
无法工作,因为您无法将 Test
转换为 (int, int)
。它将生成一个编译器错误:
Cannot implicitly convert type 'Deconstruct.Test' to '(int, int)'
有没有办法让元组列表解构为 List<T>
?
我在使用以下代码示例时遇到以下编译错误:
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....
}
}
}
您需要明确地将 testList
(List<Test>
) 转换为 tupleList
(List<(int, int)>
)
tupleList = testList.Select(t => (t.A, t.B)).ToList();
解释:
您正在使用代码,就好像 Deconstruct
允许您将实现 Deconstruct
的 class 转换为元组 (ValueTuple
),但这不是 Deconstruct
剂量。
来自文档 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.
解构returns多个元素为单个变量,不是元组(ValueTuple
).
正在尝试将 List<Test>
转换为 List<(int, int)>
,如下所示:
var testList = new List<Test>();
var tupleList = new List<(int, int)>();
tupleList = testList;
无法工作,因为您无法将 List<Test>
转换为 List<(int, int)>
。它将产生一个编译器错误:
Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List<(int, int)>'
正在尝试将每个 Test
元素转换为 (int, int)
,如下所示:
tupleList = testList.Cast<(int, int)>().ToList();
无法工作,因为您无法将 Test
转换为 (int, int)
。它将生成 run-time 错误:
System.InvalidCastException: 'Specified cast is not valid.'
正在尝试将单个 Test
元素转换为 (int, int)
,如下所示:
(int, int) tuple = test;
无法工作,因为您无法将 Test
转换为 (int, int)
。它将生成一个编译器错误:
Cannot implicitly convert type 'Deconstruct.Test' to '(int, int)'