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

Description

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

Syntax

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


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

Parameters

Array.LastIndexOf(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 backward search.

Returns

Array.LastIndexOf(T[], T, Int32) method returns The zero-based index of the last occurrence of value within the range of elements in array that extends from the first element to startIndex, if found; otherwise, -1.

Example

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


/*from ww w .  j  a v  a  2  s .com*/
using System;

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


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

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

        Console.WriteLine(Array.LastIndexOf(myValues, "2001", 4, 4));
    }
}

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