我的理解是,如果我想获取列表中某个项目的 ID,我可以这样做:
private static void a()
{
List<string> list = new List<string> {"Box", "Gate", "Car"};
Predicate<string> predicate = new Predicate<string>(getBoxId);
int boxId = list.FindIndex(predicate);
}
private static bool getBoxId(string item)
{
return (item == "box");
}
但是如果我想让比较动态呢?因此,我不想检查 item=="box",而是想将用户输入的字符串传递给委托,并检查 item==searchString。