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

前端 未结 6 1659
遥遥无期
遥遥无期 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:39

    The answer is for those coming here to know why IndexOf() doesn't work.

    Your class must override Equals method of object possessing the following declaration.

    public override bool Equals(object obj)
    
    0 讨论(0)
  • 2020-12-29 01:46

    you can do this through override Equals method

    class Employee
        {
            string _name;
            string _last;
            double _val;
            public Employee(string name, string last, double  val)
            {
                _name = name;
                _last = last;
                _val = val;
            }
            public override bool Equals(object obj)
            {
                Employee e = obj as Employee;
                return e._name == _name;
            }
        }
    
    0 讨论(0)
  • 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<Employee> employeeList = new List<Employee>();
            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();
        }
    }
    
    0 讨论(0)
  • 2020-12-29 01:57
    public int FindIndex(Predicate<T> match);
    

    Using lambdas:

    employeeList.FindIndex(r => r.LastName.Equals("Something"));
    

    Note:

    // Returns:
    //     The zero-based index of the first occurrence of an element
    //     that matches the conditions defined by match, if found; 
    //     otherwise, –1.
    
    0 讨论(0)
  • 2020-12-29 01:58

    I prefer like this

        private List<Person> persons = List<Person>();
    
                public PersonService()
                {
                    persons = new List<Person>() { 
                        new Person { Id = 1, DOB = DateTime.Today, FirstName = "Pawan", LastName = "Shakya" },
                        new Person { Id = 2, DOB = DateTime.Today, FirstName = "Bibek", LastName = "Pandey" },
                        new Person { Id = 3, DOB = DateTime.Today, FirstName = "Shrestha", LastName = "Prami" },
                        new Person { Id = 4, DOB = DateTime.Today, FirstName = "Monika", LastName = "Pandey" },
                    };
                }
    
    public PersonRepository.Interface.Person GetPerson(string lastName)
            {
                return persons[persons.FindIndex(p=>p.LastName.Equals(lastName, StringComparison.OrdinalIgnoreCase))];
            }
    
    0 讨论(0)
  • 2020-12-29 02:02
    int index = employeeList.FindIndex(employee => employee.LastName.Equals(somename, StringComparison.Ordinal));
    

    Edit: 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);
        });
    
    0 讨论(0)
提交回复
热议问题