gpt4 book ai didi

c# - System.Collections.Generic.List 需要 '1' 类型参数

转载 作者:可可西里 更新时间:2023-11-01 08:23:22 27 4
gpt4 key购买 nike

我在以下代码中遇到此错误:

string[] colors = { "green", "brown", "blue", "red" };
var list = new List(colors);
IEnumerable query = list.Where(c => c.length == 3);
list.Remove("red");
Console.WriteLine(query.Count());

此外,Count() 似乎不再被允许。它被弃用了吗?

最佳答案

您正在尝试创建一个 List<string>你应该告诉编译器

var list = new List<string>(colors);

没有List , 有一个名为 List<T> 通用类, 需要类型参数。如果不指定类型参数,则无法创建泛型列表。

您还试图调用 Count扩展方法。该方法采用 IEnumerable<T>作为第一个参数,不是 IEnumerable ,这里是定义:

public static int Count<TSource>(this IEnumerable<TSource> source)

所以你应该使用IEnumerable<string>访问该扩展方法:

IEnumerable<string> query = list.Where(c => c.Length == 3);
list.Remove("red");
Console.WriteLine(query.Count());

关于c# - System.Collections.Generic.List<T> 需要 '1' 类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21705342/

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