C# Array IndexOf(Array, Object, Int32)

Description

Array IndexOf(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.

Syntax

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


public static int IndexOf(
  Array array,/*from   w w  w.j a v a2s .c om*/
  Object value,
  int startIndex
)

Parameters

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

  • array - The one-dimensional Array to search.
  • value - The object to locate in array.
  • startIndex - The starting index of the search. 0 (zero) is valid in an empty array.

Returns

Array.IndexOf(Array, Object, Int32) method returns 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.

Example

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


using System;//from w  w  w.  ja  va2 s  .c  o m
public class SamplesArray
{

    public static void Main()
    {
        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);

        int myIndex = Array.IndexOf(myArray, "in", 4);
        Console.WriteLine(myIndex);

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version