【问题标题】:C# - Add multiple elements to an IList in one lineC# - 在一行中将多个元素添加到 IList
【发布时间】:2014-08-06 10:20:20
【问题描述】:

我可以使用 Java 在一行中做到这一点。

Java:

List<WebElement> colElements;
WebElement rowElement;

//some code

colElements.addAll(rowElement.findElements(By.tagName("td")));

C#:

IList<IWebElement> colElements;
IWebElement rowElement;

//some code

colElements.addAll(rowElement.FindElements(By.TagName("td")));

显然,在 C# 的 IList 接口中没有名为 addAll 的方法。

我希望有另一种方法可以在一行中做到这一点。

谢谢大家!

【问题讨论】:

    标签: java c# list arraylist


    【解决方案1】:

    你想要的是AddRange 方法。

    colElements.AddRange(rowElement.FindElements(By.TagName("td")));
    

    有关此方法的更多文档,请查看here

    【讨论】:

    • 这仅在 List 类中可用,而不是我在这里使用的 IList 接口。
    • @John 您使用IList 而非List 有什么特别的原因吗?
    • 是的,但我想你应该初始化一个列表来添加你的元素。否则你可以在哪里添加你的元素?列表实现了IList 接口。
    • 我需要使用 IList,否则我的其他代码:colElements = rowElement.FindElements(By.TagName("th")); 将不起作用。
    • @John 如果有的话会抛出什么错误? List 实现 IListList 应该始终是首选,除非您将代码暴露在框架或其他东西中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多