List <T> .IndexOf () efficiency compared to List <T> .FindIndex ()

Which method

  • List<T>.IndexOf() and
  • List<T>.FindIndex()

more efficient in terms of processing time?

Type T in this case String .

+6
source share
1 answer

IndexOf runs the for loop using the Equals implementation of the objects that are searched for a match. FindIndex also forms a for loop, but evaluates a Predicate to check for compliance.

Each of them comes down to a for loop. The difference in performance, if any, will be negligible. Here are some excerpts from MSDN:

List<T>.IndexOf Method (T) :

This method performs a linear search; therefore, this method is an O (n) operation, where n is Count .

List<T>.FindIndex Method (Predicate<T>) :

This method performs a linear search; therefore, this method is an O (n) operation, where n is Count .

However, the two functions will be used in a completely different way. The first assumes that you have an object from the list, and you just need to know at what index it exists (if any) in the list.

The latter assumes that you know some criteria about the object, and you want to find the first index where the object in the list matches these criteria. There may be several matches, but the method returns the first match.

+9
source

All Articles