如何使用List< object>的IndexOf()方法? [英] How to use IndexOf() method of List<object>
本文介绍了如何使用List< object>的IndexOf()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看到的所有在List<T>
中使用IndexOf()
方法的示例都是基本字符串类型.我想知道的是如何基于对象变量之一返回作为对象的列表类型的索引.
All the examples I see of using the IndexOf()
method in List<T>
are of basic string types. What I want to know is how to return the index of a list type that is an object, based on one of the object variables.
List<Employee> employeeList = new List<Employee>();
employeeList.Add(new Employee("First","Last",45.00));
我想找到employeeList.LastName == "Something"
推荐答案
int index = employeeList.FindIndex(employee => employee.LastName.Equals(somename, StringComparison.Ordinal));
不使用C#2.0的lambda(原始语言不使用LINQ或任何.NET 3+功能,而仅使用C#3.0中的lambda语法):
Without lambdas for C# 2.0 (the original doesn't use LINQ or any .NET 3+ features, just the lambda syntax in C# 3.0):
int index = employeeList.FindIndex(
delegate(Employee employee)
{
return employee.LastName.Equals(somename, StringComparison.Ordinal);
});
这篇关于如何使用List< object>的IndexOf()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文