C# Array IndexOf(T[], T, Int32)

Description

Array IndexOf(T[], T, Int32)searches for the specified object and returns the index of the first occurrence within the range of elements in the Array that extends from the specified index to the last element.

Syntax

Array.IndexOf(T[], T, Int32) has the following syntax.


public static int IndexOf<T>(
  T[] array,/*  ww w. j av  a  2  s .c o  m*/
  T value,
  int startIndex
)

Parameters

Array.IndexOf(T[], T, Int32) has the following parameters.

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

Returns

Array.IndexOf(T[], T, Int32) method returns The zero-based 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, -1.

Example

The following code example demonstrates all three generic overloads of the IndexOf method.


/*from w w  w. j  av  a2s  . c  o  m*/
using System;

public class Example
{
    public static void Main()
    {
        string[] myValues = { "2001",
            "1999",
            "2000",
            "1234",
            "2002",
            "2001",
            "4567" };


        Console.WriteLine(Array.IndexOf(myValues, "2001"));

        Console.WriteLine(Array.IndexOf(myValues, "2001", 3));

        Console.WriteLine(Array.IndexOf(myValues, "2001", 2, 2));
    }
}

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