C# ArrayList IndexOf(Object, Int32)

Description

ArrayList IndexOf(Object, Int32) searches for the specified Object and returns the zero-based index of the first occurrence within the range of elements in the ArrayList that extends from the specified index to the last element.

Syntax

ArrayList.IndexOf(Object, Int32) has the following syntax.


public virtual int IndexOf(
  Object value,
  int startIndex
)

Parameters

ArrayList.IndexOf(Object, Int32) has the following parameters.

  • value - The Object to locate in the ArrayList. The value can be null.
  • startIndex - The zero-based starting index of the search. 0 (zero) is valid in an empty list.

Returns

ArrayList.IndexOf(Object, Int32) method returns The zero-based index of the first occurrence of value within the range of elements in the ArrayList that extends from startIndex to the last element, if found; otherwise, -1.

Example

Search for the first occurrence of the duplicated value in the last section of the ArrayList.


using System;/*  w  w  w. j  ava2  s .  c o m*/
using System.Collections;
public class SamplesArrayList
{

    public static void Main()  
    {
        ArrayList myAL = new ArrayList();
        myAL.Add( "the" );
        myAL.Add( "quick" );
        myAL.Add( "brown" );
        myAL.Add( "fox" );
        myAL.Add( "the" );
  
        String myString = "the";
        int myIndex = -1;

        myIndex = myAL.IndexOf( myString, 4 );
        Console.WriteLine(myIndex );
    }

    public static void PrintIndexAndValues(IEnumerable myList)
    {
        int i = 0;
        foreach (Object obj in myList)
            Console.WriteLine("   [{0}]:    {1}", i++, obj);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections »




ArrayList
BitArray
Comparer
Hashtable
Queue
SortedList
Stack