C# List LastIndexOf(T)

Description

List LastIndexOf(T) searches for the specified object and returns the zero-based index of the last occurrence within the entire List .

Syntax

List.LastIndexOf(T) has the following syntax.


public int LastIndexOf(
  T item
)

Parameters

List.LastIndexOf(T) has the following parameters.

  • item - The object to locate in the List . The value can be null for reference types.

Returns

List.LastIndexOf(T) method returns The zero-based index of the last occurrence of item within the entire the List, if found; otherwise, -1.

Example

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


//from   ww  w. j  a v a 2  s . c  o  m
using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        List<string> myData = new List<string>();

        myData.Add("A");
        myData.Add("B");
        myData.Add("C");
        myData.Add("D");
        myData.Add("A");

        foreach(string myD in myData)
        {
            Console.WriteLine(myD);
        }

        Console.WriteLine(myData.LastIndexOf("A"));

        Console.WriteLine(myData.LastIndexOf("A", 3));

        Console.WriteLine(myData.LastIndexOf("A", 4, 4));
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Collections.Generic »




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack