C# | Searching the index of specified object in Collection<T> Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report 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>. Syntax: 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: Example 1: CSHARP // C# code to search for the specified // object and returns the zero-based // index of the first occurrence within // the entire Collection using System; using System.Collections.Generic; using System.Collections.ObjectModel; class GFG { // Driver code public static void Main() { // Creating a collection of strings Collection<string> myColl = new Collection<string>(); // Adding elements in Collection myColl myColl.Add("A"); myColl.Add("B"); myColl.Add("C"); myColl.Add("D"); myColl.Add("D"); myColl.Add("E"); // Displaying the elements in myColl foreach(string str in myColl) { Console.WriteLine(str); } // Searching for the specified object // and returns the zero-based index of // the first occurrence within the entire // Collection. If the object doesn't contain the // object, then -1 is returned Console.WriteLine("Index : " + myColl.IndexOf("D")); } } Output: A B C D D E Index : 3 Example 2: CSHARP // C# code to search for the specified // object and returns the zero-based // index of the first occurrence within // the entire Collection using System; using System.Collections.Generic; using System.Collections.ObjectModel; class GFG { // Driver code public static void Main() { // Creating a collection of ints Collection<int> myColl = new Collection<int>(); // Adding elements in Collection myColl myColl.Add(2); myColl.Add(3); myColl.Add(4); myColl.Add(5); // Displaying the elements in myColl foreach(int i in myColl) { Console.WriteLine(i); } // Searching for the specified object // and returns the zero-based index of // the first occurrence within the entire // Collection. If the object doesn't contain the // object, then -1 is returned Console.WriteLine("Index : " + myColl.IndexOf(7)); } } Output: 2 3 4 5 Index : -1 Note: 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. Reference: https://docs.microsoft.com/en-us/dotnet/api/system.collections.objectmodel.collection-1.indexof?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Searching the index of specified object in Collection<T> S Sahil_Bansall Follow Improve Article Tags : C# CSharp-method CSharp-Collections.ObjectModel-Namespace CSharp-Collection-Class Similar Reads C# | Get or set the element at specified index in Collection<T> Collection<T>.Item[Int32] property is used to get or set the element at the specified index. Syntax: public T this[int index] { get; set; } Here, index is the zero-based index of the element to get or set. Return Value: The element at the specified index. Exception: This method will give Argum 3 min read C# | Getting the index of the specified key in a SortedList object SortedList.IndexOfKey(Object) Method is used to get the zero-based index of the specified key in a SortedList object. Syntax: public virtual int IndexOfKey (object key); Here, key is the Key which is to be located in the SortedList object. Return Value: This method returns the zero-based index of ty 3 min read C# | Getting index of the specified value in a SortedList object SortedList.IndexOfValue(Object) Method is used to get the zero-based index of the first occurrence of the specified value in a SortedList object. Syntax: public virtual int IndexOfValue (object value); Here, value is the Value which is to be located in the SortedList object. The value can be null. R 3 min read C# | Remove element at specified index of Collection<T> Collection<T>.RemoveAt(Int32) is used to remove the element at the specified index of the Collection<T>. Syntax: public void RemoveAt (int index); Here, index is the zero-based index of the element to remove. Exception: This method will give ArgumentOutOfRangeException if the index is le 2 min read Finding the Index of First Element of the Specified Sequence in C# The Index Structure is introduced in C# 8.0. It represents a type that can be used to index a collection or sequence and it can be started from the start or the end. You are allowed to find the index, which points the first element of the specified collection or sequence with the help of Start Prope 2 min read Like