【问题标题】:Converting from one array to another array in C#在 C# 中从一个数组转换为另一个数组
【发布时间】:2015-10-20 11:50:10
【问题描述】:


我在 C# 中有这段代码,可以从一个数组转换为另一个数组:

IWebElement[] elements = Self.FindChildren()
Step[] steps = new Step[elements.Length];
for (int i = 0; i < elements.Length; i++)
{
   steps[i] = new Step(elements[i]);
}

如何以更短的方式编写它(使用 linq 或 lambda 表达式)?
谢谢
奥马尔

【问题讨论】:

    标签: c# arrays linq


    【解决方案1】:

    Linq 接近

    IWebElement[] elements = Self.FindChildren();
    Step[] steps = elements.Select(x => new Step(x)).ToArray();
    

    更快但没有Linq

    IWebElement[] elements = Self.FindChildren()
    Step[] steps = new Step[elements.Length];
    Array.Copy(elements, steps, elements.Length);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-17
      • 2020-06-05
      • 1970-01-01
      • 2011-02-16
      • 2021-10-03
      • 2021-06-28
      • 1970-01-01
      相关资源
      最近更新 更多