C# Array IndexOf(T[], T)

Description

Array IndexOf(T[], T)searches for the specified object and returns the index of the first occurrence within the entire Array.

Syntax

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


public static int IndexOf<T>(
  T[] array,
  T value
)

Parameters

Array.IndexOf(T[], T) 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.

Returns

Array.IndexOf(T[], T) method returns The zero-based index of the first occurrence of value within the entire array, if found; otherwise, -1.

Example

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


/*from   w  ww.  j a  v  a2 s . com*/
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