Collection<T>.IndexOf(T) method is used to search for the specified object and returns the zero-based index of the first occurrence within the entire Collection<T>.
public int IndexOf (T item);
Here, item is the object to locate in the List<T>. The value can be null for reference types.
Return Value: This method returns the zero-based index of the first occurrence of item within the entire Collection<T>, if found, otherwise, -1.
Below given are some examples to understand the implementation in a better way:
A B C D D E Index : 3
2 3 4 5 Index : -1
- The Collection<T> is searched forward starting at the first element and ending at the last element.
- This method determines equality using the default equality comparer EqualityComparer<T>.Default for T, the type of values in the list.
- This method performs a linear search. Therefore, this method is an O(n) operation, where n is Count.
- C# | Getting index of the specified value in a SortedList object
- C# | Getting the key at the specified index of a SortedList object
- C# | Getting the index of the specified key in a SortedList object
- C# | Getting the value at the specified index of a SortedList object
- C# | Replacing the value at a specific index in a SortedList object
- Creating an Index From the End of a Collection at a Specified Index Position in C#
- Creating an Index From the Specified Index at the Start of a Collection in C#
- C# | Check if an array object is equal to another array object
- How to get the index value in C#?
- Index Struct in C# 8.0
- Index Constructor in C#
- C# | Get or Set at specified index in StringCollection
- C# | Get or set the element at specified index in Collection<T>
- C# | Index of first occurrence in StringCollection
- C# | Insert into OrderedDictionary with key and value at specified index
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.