C# ArrayList - IndexOf() Method



The C# ArrayList IndexOf() method is used to search the index of the first occurrence of a specific element in the ArrayList. If the element is not found in the ArrayList, it returns -1.

Syntax

Following are the syntax of the C# ArrayList IndexOf() method −

It finds the index of the first occurrence of the given element in the entire ArrayList.

public virtual int IndexOf(object value);

It searches for the given element, starting from a given index in the ArrayList.

public virtual int IndexOf(object value, int startIndex);

It searches for the given element in a specific range of elements, beginning with the startIndex and continues to the next count of elements.

public virtual int IndexOf(object value, int startIndex, int count);

Parameters

This method accepts the following parameters according to all overloaded syntax −

  • value: It is an element of an ArrayList to be searched for.
  • startIndex: It specifies the start index in ArrayList, and from there, searches will start.
  • count: The number of elements to be searched in ArrayList.

Return value

This method returns the index value of the first occurrence of the value; otherwise returns -1.

Example 1: Using IndexOf(object value)

Following is the basic example of the IndexOf() method. Here, we use the default syntax to search for an element from the entire ArrayList −

using System;
using System.Collections;
class Example {
  static void Main() {
    // Create an ArrayList
    ArrayList arrayList = new ArrayList {
       "A", "B", "C", "D", "B"
    };

    // Find the index of the first occurrence of "B"
    int index = arrayList.IndexOf("B");

    Console.WriteLine($"The first occurrence of 'B' is at index: {index}");
  }
}

Output

Following is the output −

The first occurrence of 'B' is at index: 1

Example 2: Using IndexOf(object value, int startIndex)

Let's see another example of the IndexOf() method. Here, we search for an element in ArrayList from the given startIndex −

using System;
using System.Collections;

class Example {
   static void Main() {
      // Create an ArrayList
      ArrayList arrayList = new ArrayList {
         "B", "A", "B", "C", "D"
      };
      
      // Find the index of the first occurrence of "B"
      // starting from index 1
      int index = arrayList.IndexOf("B", 1);
      Console.WriteLine($"The first occurrence of 'B' is at index: {index}");
   }
}

Output

Following is the output −

The first occurrence of 'B' is at index: 2

Example 3: Using IndexOf(object value, int startIndex, int count)

Following is another version of the IndexOf() method. Here, we search for an element in ArrayList within a specific number of elements that will start from the given start index −

using System;
using System.Collections;
class Program
{
   static void Main()
   {
      ArrayList arrayList = new ArrayList { "A", "C", "E", "B", "B" };

      // Find the index of "B" starting from index 1,
      // within the next 3 elements
      int index = arrayList.IndexOf("B", 1, 3);

      Console.WriteLine($"The first occurrence of 'B' in the specified range is at index: {index}");
   }
}

Output

Following is the output −

The first occurrence of 'B' in the specified range is at index: 3

Example 4: Search A Object in Person Class

The following example uses the IndexOf method to search for a person object that is available in the ArrayList. If so, the method returns the index −

using System;
using System.Collections;

class Example
{
   public string Name { get; set; }
   public int Age { get; set; }

   // Override Equals to define equality for Person objects
   public override bool Equals(object obj)
   {
      if (obj is Example other)
      {
         return this.Name == other.Name && this.Age == other.Age;
      }
      return false;
   }
   public override int GetHashCode()
   {
      return HashCode.Combine(Name, Age);
   }
}
class Program
{
   static void Main()
   {
      // Create an ArrayList of Person objects
      ArrayList people = new ArrayList
      {
         new Example { Name = "Aman", Age = 25 },
         new Example { Name = "Akash", Age = 30 },
         new Example { Name = "Rahul", Age = 35 }
      };

      // Create a Person object to search for
      Example personToFind = new Example { Name = "Akash", Age = 30 };

      int index = people.IndexOf(personToFind);
      if (index != -1)
      {
         Console.WriteLine($"Person found at index: {index}");
      }
      else
      {
         Console.WriteLine("Person not found in the list.");
      }
   }
}

Output

Following is the output −

Person found at index: 1
csharp_arraylist.htm
Advertisements