【问题标题】:How get a list from an array如何从数组中获取列表
【发布时间】:2015-06-26 19:28:45
【问题描述】:

我创建了一个列表数组。 但是当尝试迭代数组时会出现转换类型错误。

List<Piece> [] CombinationBottom1;

foreach (List<Piece> lp in CombinationTop1[0])
{
}

无法将类型“Test.Piece”转换为“System.Collections.Generic.List”

CombinationTop1[0] 不是 List&lt;Piece&gt; 吗??

我可以创建一个List&lt;List&lt;Piece&gt;&gt; 但我想访问第 N 个元素并且使用数组更容易。

例如这个工作没问题:

List<List<Piece>> CombinationBottom1;

foreach (List<Piece> lp in CombinationTop1)
{
}

【问题讨论】:

  • 那是因为CombinationTop1[0]是一个列表...你应该这样做foreach (Piece p in CombinationTop1[0])
  • 您的代码不相似。第一个应该是foreach (List&lt;Piece&gt; lp in CombinationBottom1)foreach(Piece lp in CombinationBottom1[0])
  • 我明白我为什么感到困惑了。现在我创建另一个class L_Pieces = List&lt;Piece&gt; 所以第一个是List&lt;L_Piece&gt; [] CombinationBottom1

标签: c# arrays winforms


【解决方案1】:

那是因为CombinationTop1[0] 是一个列表......你应该这样做

List<Piece> [] CombinationBottom1;

foreach (Piece p in CombinationTop1[0])
{
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2012-02-27
    • 2020-05-18
    • 1970-01-01
    相关资源
    最近更新 更多