C# List Find
上次修改时间:2025 年 4 月 11 日
本教程演示了如何使用 Find、FindLast、FindAll、FindIndex 和 FindLastIndex 方法在 C# List 中定位元素。
在 C# 中,List 是一种动态集合,它保存相同类型的元素,可以通过它们的索引位置访问。
C# List Find
Find 方法检索 List 中满足指定条件的第一个元素,该条件称为谓词。
谓词是一个函数,它接受一个参数并返回一个布尔值,指示该元素是否满足条件。
public T? Find(Predicatematch);
Find 方法接受 Predicate<T> 委托,它定义了要匹配的条件。
var words = new List<string> { "sky", "cup", "new", "war", "wrong",
"crypto", "forest", "water", "cup" };
var vals = new List<int> { -2, -1, 3, 0, 1, 2, 1, 4, -2, 2, 1 };
string? e = words.Find(e => e.StartsWith("w"));
Console.WriteLine(e);
int n = vals.Find(e => e > 0);
Console.WriteLine(n);
此示例演示了在字符串列表中查找以“w”开头的第一个单词,以及在数字列表中查找第一个正整数。
string? e = words.Find(e => e.StartsWith("w"));
在这里,谓词是一个 lambda 表达式,用于检查单词是否以“w”开头。
$ dotnet run war 3
C# List FindLast
FindLast 方法返回 List 中与给定谓词匹配的最后一个元素。
public T? FindLast(Predicatematch);
这定义了该方法的签名。
var words = new List<string> { "sky", "cup", "new", "war", "wrong",
"crypto", "forest", "water", "cup" };
var vals = new List<int> { -2, -1, 3, 0, 1, 2, 1, 4, -2, 2, 1 };
string? e = words.FindLast(e => e.StartsWith("w"));
Console.WriteLine(e);
int n = vals.FindLast(e => e > 0);
Console.WriteLine(n);
该程序识别字符串列表中以“w”开头的最后一个单词,以及数字列表中最后一个正整数。
$ dotnet run water 1
C# List FindAll
FindAll 方法检索 List 中满足指定谓词的所有元素,并将它们作为新的 List 返回。
var words = new List<string> { "sky", "cup", "new", "war", "wrong",
"crypto", "forest", "water", "cup" };
var vals = new List<int> { -2, -1, 3, 0, 1, 2, 1, 4, -2, 2, 1 };
List<string> res = words.FindAll(e => e.StartsWith("w"));
Console.WriteLine(string.Join(',', res));
List<int> res2 = vals.FindAll(e => e > 0);
Console.WriteLine(string.Join(',', res2));
此示例从字符串列表中收集所有以“w”开头的单词,并从数字列表中收集所有正整数,然后将它们显示为逗号分隔的字符串。
$ dotnet run war,wrong,water 3,1,2,1,4,2,1
C# List FindIndex
FindIndex 方法返回与谓词匹配的第一个元素的从零开始的索引。如果未找到任何元素,则返回 -1。
public int FindIndex(Predicatematch); public int FindIndex(int startIndex, Predicate match); public int FindIndex(int startIndex, int count, Predicate match);
这些重载方法允许指定起始索引,以及可选的要搜索的元素数量。
var words = new List<string> { "sky", "cup", "new", "war", "wrong",
"crypto", "forest", "water", "cup" };
int n = words.FindIndex(e => e.StartsWith("w"));
Console.WriteLine($"The index of the first word starting with 'w' is {n}");
int n2 = words.FindIndex(5, e => e.StartsWith("w"));
Console.WriteLine($"The index of the first word starting with 'w' after index 5 is {n2}");
该程序查找从列表开头和从索引 5 开始的第一个以“w”开头的单词的索引。
$ dotnet run The index of the first word starting with 'w' is 3 The index of the first word starting with 'w' after index 5 is 7
C# List FindLastIndex
FindLastIndex 方法返回与谓词匹配的最后一个元素的从零开始的索引。如果未找到匹配项,则返回 -1。
public int FindLastIndex(Predicate<T> match); public int FindLastIndex(int startIndex, Predicate<T> match); public int FindLastIndex(int startIndex, int count, Predicate<T> match);
这些是方法签名,允许控制搜索范围。
var words = new List<string> { "sky", "cup", "new", "war", "wrong",
"crypto", "forest", "water", "cup" };
int n = words.FindLastIndex(e => e.StartsWith("w"));
Console.WriteLine($"The index of the last word starting with 'w' is {n}");
int n2 = words.FindLastIndex(5, e => e.StartsWith("w"));
Console.WriteLine($"The index of the last word starting with 'w' up to index 5 is {n2}");
此示例查找整个列表中以“w”开头的最后一个单词的索引,以及索引 5 之前的最后一个单词的索引。
$ dotnet run The index of the last word starting with 'w' is 7 The index of the last word starting with 'w' up to index 5 is 4
来源
本教程演示了如何使用各种查找方法在 C# List 中定位元素。
作者
浏览所有 C# 教程。