C# 在列表中查找对象参数 x 等于值的对象的索引
C# Find the Index of an Object in List where Object Parameter x equals Value
我需要在参数x等于某个值的对象列表中找到对象的索引值。
有没有简单的方法可以做到这一点?我了解 IndexOf 函数,但如何将其应用于对象参数?
您可以使用 List.FindIndex
:
int index = list.FindIndex(obj => obj.X == value);
The zero-based index of the first occurrence of an element that
matches the conditions defined by match, if found; otherwise, –1.
我需要在参数x等于某个值的对象列表中找到对象的索引值。
有没有简单的方法可以做到这一点?我了解 IndexOf 函数,但如何将其应用于对象参数?
您可以使用 List.FindIndex
:
int index = list.FindIndex(obj => obj.X == value);
The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, –1.