How to use IndexOf() method of List<object>

前端 未结 6 1688
遥遥无期
遥遥无期 2020-12-29 01:08

All the examples I see of using the IndexOf() method in List are of basic string types. What I want to know is how to return the index of

6条回答
  •  半阙折子戏
    2020-12-29 01:56

    Sorry, one more for good measure :)

    int index = employees.FindIndex(
          delegate(Employee employee)
            {
               return employee.LastName == "Something";
            });
    

    Edit: - Full Example in .NET 2.0 Project.

    class Program
    {
        class Employee { public string LastName { get; set; } }
        static void Main(string[] args)
        {
            List employeeList = new List();
            employeeList.Add(new Employee(){LastName="Something"});
            employeeList.Add(new Employee(){LastName="Something Else"});
            int index = employeeList.FindIndex(delegate(Employee employee) 
                               { return employee.LastName.Equals("Something"); });
            Console.WriteLine("Index:{0}", index);
            Console.ReadKey();
        }
    }
    

提交回复
热议问题