Searching the index of specified object in Collection in C#

In C#, you can search for the index of a specified object in a collection using the IndexOf() method. This method returns the zero-based index of the first occurrence of the specified object, or -1 if the object is not found.

Syntax

The basic syntax for finding the index of an object in a collection −

int index = collection.IndexOf(objectToFind);

Return Value

  • Returns the zero-based index of the first occurrence of the specified object.

  • Returns -1 if the object is not found in the collection.

Using IndexOf() with StringCollection

The following example demonstrates how to find the index of specific objects in a StringCollection

using System;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      StringCollection strCol = new StringCollection();
      strCol.Add("Accessories");
      strCol.Add("Books");
      strCol.Add("Electronics");
      strCol.Add("Books");
      
      Console.WriteLine("StringCollection elements...");
      foreach (string res in strCol) {
         Console.WriteLine(res);
      }
      
      strCol.Insert(2, "Headphone");
      Console.WriteLine("StringCollection elements...UPDATED");
      foreach (string res in strCol) {
         Console.WriteLine(res);
      }
      
      Console.WriteLine("Index of specific object Electronics? = " + strCol.IndexOf("Electronics"));
   }
}

The output of the above code is −

StringCollection elements...
Accessories
Books
Electronics
Books
StringCollection elements...UPDATED
Accessories
Books
Headphone
Electronics
Books
Index of specific object Electronics? = 3

Finding Index in Collections Initialized with Arrays

This example shows how to search for object indices in a StringCollection initialized with an array −

using System;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      StringCollection stringCol = new StringCollection();
      String[] arr = new String[] { "100", "200", "300", "400", "500" };
      
      Console.WriteLine("Array elements...");
      foreach (string res in arr) {
         Console.WriteLine(res);
      }
      
      stringCol.AddRange(arr);
      Console.WriteLine("Does the specified string is in the StringCollection? = " + stringCol.Contains("800"));
      Console.WriteLine("Total number of elements = " + stringCol.Count);
      Console.WriteLine("Iterating through StringCollection = " + stringCol.Count);
      
      StringEnumerator myenum = stringCol.GetEnumerator();
      while (myenum.MoveNext())
         Console.WriteLine(myenum.Current);
      
      Console.WriteLine("Index of specific object 500? = " + stringCol.IndexOf("500"));
      Console.WriteLine("Index of specific object 1000? = " + stringCol.IndexOf("1000"));
   }
}

The output of the above code is −

Array elements...
100
200
300
400
500
Does the specified string is in the StringCollection? = False
Total number of elements = 5
Iterating through StringCollection = 5
100
200
300
400
500
Index of specific object 500? = 4
Index of specific object 1000? = -1

Using IndexOf() with Generic Collections

The IndexOf() method also works with generic collections like List<T>

using System;
using System.Collections.Generic;

public class Demo {
   public static void Main() {
      List<string> fruits = new List<string> { "Apple", "Banana", "Cherry", "Banana", "Date" };
      
      Console.WriteLine("List elements:");
      foreach (string fruit in fruits) {
         Console.WriteLine(fruit);
      }
      
      Console.WriteLine("Index of 'Banana': " + fruits.IndexOf("Banana"));
      Console.WriteLine("Index of 'Grape': " + fruits.IndexOf("Grape"));
      Console.WriteLine("Index of 'Cherry': " + fruits.IndexOf("Cherry"));
   }
}

The output of the above code is −

List elements:
Apple
Banana
Cherry
Banana
Date
Index of 'Banana': 1
Index of 'Grape': -1
Index of 'Cherry': 2

Conclusion

The IndexOf() method is an essential tool for finding the position of objects in C# collections. It returns the zero-based index of the first occurrence of the specified object, or -1 if the object is not found. This method works with various collection types including StringCollection, List<T>, and other indexed collections.

Updated on: 2026-03-17T07:04:36+05:30

195 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements