C# 中 ICollection 中的方法,将另一个 ICollection 的所有元素添加到其中
C# 中的 ICollection 中是否有某种方法可以添加另一个集合的所有元素? 现在我必须始终为此编写 foreach 循环:
ICollection<Letter> allLetters = ... //some initalization
ICollection<Letter> justWrittenLetters = ... //some initalization
... //some code, adding to elements to those ICollections
foreach(Letter newLetter in justWrittenLetters){
allLetters.add(newLetter);
}
我的问题是,是否有可以替代该循环的方法?例如方法 addAll(Collection c)
?所以我只会写类似的东西:
allLetters.addAll(justWrittenLetters);
Is there some method in ICollection in C# that would add all elements of another collection?
Right now I have to always write foreach cycle for this:
ICollection<Letter> allLetters = ... //some initalization
ICollection<Letter> justWrittenLetters = ... //some initalization
... //some code, adding to elements to those ICollections
foreach(Letter newLetter in justWrittenLetters){
allLetters.add(newLetter);
}
My question is, is there method that can replace that cycle? Like for example the method addAll(Collection c)
in Java ? So I would write only something like:
allLetters.addAll(justWrittenLetters);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ICollection 没有这样的方法。您有两种选择,要么使用不同的类型,例如具有 AddRange() 方法的 List,要么创建一个扩展方法:
There isn't a method like this for ICollection. You have two options, either use a different type such as List which has the AddRange() method or alternatively, create an extension method: