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

Description

Array LastIndexOf(T[], T, Int32, Int32)searches for the specified object and returns the index of the last occurrence within the range of elements in the Array that contains the specified number of elements and ends at the specified index.

Syntax

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


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

Parameters

Array.LastIndexOf(T[], T, Int32, 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.
  • count - The number of elements in the section to search.

Returns

Array.LastIndexOf(T[], T, Int32, Int32) method returns The zero-based index of the last occurrence of value within the range of elements in array that contains the number of elements specified in count and ends at startIndex, if found; otherwise, -1.

Example

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


//from w w  w .j a  v a 2 s  .c  o  m
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