C# ArrayList IndexOf(Object)

Description

ArrayList IndexOf(Object) searches for the specified Object and returns the zero-based index of the first occurrence within the entire ArrayList.

Syntax

ArrayList.IndexOf(Object) has the following syntax.


public virtual int IndexOf(
  Object value
)

Parameters

ArrayList.IndexOf(Object) has the following parameters.

  • value - The Object to locate in the ArrayList. The value can be null.

Returns

ArrayList.IndexOf(Object) method returns The zero-based index of the first occurrence of value within the entire ArrayList, if found; otherwise, -1.

Example

The following code searches for the first occurrence of the duplicated value.


using System;/*w  ww  .  jav  a2  s.c  om*/
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( "jumps" );
        myAL.Add( "over" );
        myAL.Add( "the" );
        String myString = "the";
        int myIndex = myAL.IndexOf( myString );
        Console.WriteLine( "The first occurrence of \"{0}\" is at index {1}.", myString, 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