Export (0) Print
Expand All

Array.IndexOf Method (Array, Object, Int32)

Searches for the specified object and returns the index of the first occurrence within the range of elements in the one-dimensional Array that extends from the specified index to the last element.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static int IndexOf(
	Array array,
	Object value,
	int startIndex
)

Parameters

array
Type: System.Array

The one-dimensional Array to search.

value
Type: System.Object

The object to locate in array.

startIndex
Type: System.Int32

The starting index of the search. 0 (zero) is valid in an empty array.

Return Value

Type: System.Int32
The index of the first occurrence of value within the range of elements in array that extends from startIndex to the last element, if found; otherwise, the lower bound of the array minus 1.

ExceptionCondition
ArgumentNullException

array is null.

ArgumentOutOfRangeException

startIndex is outside the range of valid indexes for array.

RankException

array is multidimensional.

The one-dimensional Array is searched forward starting at startIndex and ending at the last element.

The elements are compared to the specified value using the Object.Equals method. If the element type is a nonintrinsic (user-defined) type, the Equals implementation of that type is used.

Since most arrays will have a lower bound of zero, this method would generally return –1 when value is not found. In the rare case that the lower bound of the array is equal to Int32.MinValue and value is not found, this method returns Int32.MaxValue, which is System.Int32.MinValue - 1.

Passing the Length of the array as the startindex will result in a return value of -1, while values greater than Length will raise an ArgumentOutOfRangeException.

This method is an O(n) operation, where n is the number of elements from startIndex to the end of array.

In the .NET Framework version 2.0, this method uses the Equals and CompareTo methods of the Array to determine whether the Object specified by the value parameter exists. In the earlier versions of the .NET Framework, this determination was made by using the Equals and CompareTo methods of the value Object itself.

The following code example shows how to determine the index of the first occurrence of a specified element.

using System;
public class SamplesArray  {

   public static void Main()  {

      // Creates and initializes a new Array with three elements of the same value.
      Array myArray=Array.CreateInstance( typeof(String), 12 );
      myArray.SetValue( "the", 0 );
      myArray.SetValue( "quick", 1 );
      myArray.SetValue( "brown", 2 );
      myArray.SetValue( "fox", 3 );
      myArray.SetValue( "jumps", 4 );
      myArray.SetValue( "over", 5 );
      myArray.SetValue( "the", 6 );
      myArray.SetValue( "lazy", 7 );
      myArray.SetValue( "dog", 8 );
      myArray.SetValue( "in", 9 );
      myArray.SetValue( "the", 10 );
      myArray.SetValue( "barn", 11 );

      // Displays the values of the Array.
      Console.WriteLine( "The Array contains the following values:" );
      PrintIndexAndValues( myArray );

      // Searches for the first occurrence of the duplicated value.
      String myString = "the";
      int myIndex = Array.IndexOf( myArray, myString );
      Console.WriteLine( "The first occurrence of \"{0}\" is at index {1}.", myString, myIndex );

      // Searches for the first occurrence of the duplicated value in the last section of the Array.
      myIndex = Array.IndexOf( myArray, myString, 4 );
      Console.WriteLine( "The first occurrence of \"{0}\" between index 4 and the end is at index {1}.", myString, myIndex );

      // Searches for the first occurrence of the duplicated value in a section of the Array.
      myIndex = Array.IndexOf( myArray, myString, 6, 5 );
      Console.WriteLine( "The first occurrence of \"{0}\" between index 6 and index 10 is at index {1}.", myString, myIndex );
   }


   public static void PrintIndexAndValues( Array myArray )  {
      for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
         Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );
   }
}
/*
This code produces the following output.

The Array contains the following values:
    [0]:    the
    [1]:    quick
    [2]:    brown
    [3]:    fox
    [4]:    jumps
    [5]:    over
    [6]:    the
    [7]:    lazy
    [8]:    dog
    [9]:    in
    [10]:    the
    [11]:    barn
The first occurrence of "the" is at index 0.
The first occurrence of "the" between index 4 and the end is at index 6.
The first occurrence of "the" between index 6 and index 10 is at index 6.
*/

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft