C# List.FindIndex
|
freeflydom
2026年1月4日 8:58
本文热度 474
|
List<T>.FindIndex 方法用于搜索与指定谓词条件匹配的元素,并返回第一个匹配元素的从零开始的索引。如果未找到匹配项,则返回 -1。
示例
using System;
using System.Collections.Generic;
public class Employee
{
public string Name { get; set; }
public int Id { get; set; }
}
public class Program
{
public static void Main()
{
List<Employee> employees = new List<Employee>
{
new Employee { Name = "Alice", Id = 1 },
new Employee { Name = "Bob", Id = 2 },
new Employee { Name = "Charlie", Id = 3 }
};
int index = employees.FindIndex(e => e.Name.StartsWith("B"));
Console.WriteLine(index); // 输出: 1
}
}
重载方法
1. FindIndex(Predicate<T> match)
2. FindIndex(int startIndex, Predicate<T> match)
3. FindIndex(int startIndex, int count, Predicate<T> match)
来源Bing
该文章在 2026/1/4 8:59:25 编辑过