gpt4 book ai didi

c# - Linq IList 接口(interface)与具体

转载 作者:太空狗 更新时间:2023-10-30 00:16:11 25 4
gpt4 key购买 nike

现在用

IList<string> listOfStrings = (new string[] {  "bob","mary"});

我们不能预成型

listOfStrings.ToList().ForEach(i => i.DoSome(i)));

我们需要 reshape 接口(interface)的具体实现

List<string> listOfStrings = ((new string[] {  "bob","mary"}).ToLIst();

然后我们可以为每个做一个

listOfStrings.ForEach(i => i.DoSome(i)));

这是因为 foreach 运算符不能与 IList 接口(interface)一起使用吗?这是为什么?

最佳答案

您没有使用 foreach运算符(operator) - 您使用的是 ForEach 方法,在 List<T> 上声明(here)还有 arrays . IList<T> 上没有这样的扩展方法或 IEnumerable<T> .你可以写一个,但我个人会使用真正的 foreach循环:

foreach (var text in listOfStrings)
{
...
}

参见 Eric Lippert 的博客 post on the topic对于比我更清晰的想法。

关于c# - Linq IList 接口(interface)与具体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8452439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com