【问题标题】:List.Find is returning an error in C# codeList.Find 在 C# 代码中返回错误
【发布时间】:2019-11-30 17:32:47
【问题描述】:
public static string[] categoryNames = new string[] { "Control", "Supplies", "Power" };
int listSelected = categoryNames.Find(item => item == "Power");

由于某种原因,我在Find 方法上收到此错误:

没有给出与'Array.Find(T[], Predicate)'的形参'match'对应的参数

我查看了许多示例,但无法弄清楚我在使用 List.Find() 时做错了什么。任何反馈表示赞赏。提前致谢!

【问题讨论】:

  • 数组不是列表——如果你想使用List.Find,那么你需要创建一个List<T>——你可以使用.ToList()来做到这一点
  • 有静态方法Array.FindArray.FindIndex,但是数组中没有实例方法Find
  • 对于数组,你可以使用IndexOf...

标签: c# list find


【解决方案1】:

数组没有Find 实例方法。如果要查找与谓词匹配的第一个元素的索引,则需要使用Array.FindIndex()

public static string[] categoryNames = new string[] { "Control", "Supplies", "Power" };
int listSelected = Array.FindIndex(categoryNames, item => item == "Power");

【讨论】:

    猜你喜欢
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    相关资源
    最近更新 更多