C# List<T>.LastIndexOf() Method with Example

C# List<T>.LastIndexOf() Method: In this tutorial, we will learn about the LastIndexOf() method of List collection with its usage, syntax, and an example using C# program. By IncludeHelp Last updated : April 15, 2023

C# List<T>.LastIndexOf() Method

List<T>.LastIndexOf() method is used to get the index of last occurrence of an element in the list.

Syntax

int List<T>.LastIndexOf(T item);
int List<T>.LastIndexOf(T item, int start_index);
int List<T>.LastIndexOf(T item, int start_index, int count);

Parameter(s)

  • item is an element of type T, whose first occurrence will be returned, if item found.
  • start_index is the starting position from where you want to find the element in the list.
  • count is the total elements from "start_index" to search the element in the list (backward).

Return Value

It returns index of the element, if element founds in the list between specified indexes, if element does not found in the list – it returns -1.

Note: Method searches element backward in the list.

Example

int list declaration:
List<int> a = new List<int>();

Adding elements:
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
a.Add(10);
a.Add(20);
a.Add(30);
a.Add(40);
a.Add(50);
    
Method calls:
a.LastIndexOf(20)   //output: 6
a.LastIndexOf(100)   //output: -1
a.LastIndexOf(20, 1)   //output: 1
a.LastIndexOf(100, 1)   //output: -1
a.LastIndexOf(20, 1, 3)   //output: -1
a.LastIndexOf(20, 0, 1)   //output: -1

C# program to get the last index an element in the list using List<T>.LastIndexOf() method

using System;
using System.Text;
using System.Collections.Generic;

namespace Test {
  class Program {
    static void printList(List <int> lst) {
      //printing elements
      foreach(int item in lst) {
        Console.Write(item + " ");
      }
      Console.WriteLine();
    }

    static void Main(string[] args) {
      //integer list
      List <int> a = new List <int> ();

      //adding elements
      a.Add(10);
      a.Add(20);
      a.Add(30);
      a.Add(40);
      a.Add(50);
      a.Add(10);
      a.Add(20);
      a.Add(30);
      a.Add(40);
      a.Add(50);

      //print the list
      Console.WriteLine("list elements...");
      printList(a);

      //using List.LastIndexOf(T item)
      //finding 20
      int index = a.LastIndexOf(20);
      if (index != -1)
        Console.WriteLine("20 found at " + index + " position.");
      else
        Console.WriteLine("20 does not found in the list");

      //finding 100
      index = a.LastIndexOf(100);
      if (index != -1)
        Console.WriteLine("100 found at " + index + " position.");
      else
        Console.WriteLine("100 does not found in the list");

      //using List.LastIndexOf(T item, int index)
      //finding 20
      index = a.LastIndexOf(20, 1); //start index is 1
      if (index != -1)
        Console.WriteLine("20 found at " + index + " position.");
      else
        Console.WriteLine("20 does not found in the list");

      //finding 100
      index = a.LastIndexOf(100, 1); //start index is 1
      if (index != -1)
        Console.WriteLine("100 found at " + index + " position.");
      else
        Console.WriteLine("100 does not found in the list");

      //using List.LastIndexOf(T item, int start_index, int count)
      //finding 20
      //search will perform from 9th index to 3 element backward
      index = a.LastIndexOf(20, 9, 3);

      if (index != -1)
        Console.WriteLine("20 found at " + index + " position.");
      else
        Console.WriteLine("20 does not found in the list");

      //finding 20
      //search will perform from 0th index to 1 element backward
      index = a.LastIndexOf(20, 0, 1);
      if (index != -1)
        Console.WriteLine("20 found at " + index + " position.");
      else
        Console.WriteLine("20 does not found in the list");

      //hit ENTER to exit
      Console.ReadLine();
    }
  }
}

Output

list elements...
10 20 30 40 50 10 20 30 40 50
20 found at 6 position.
100 does not found in the list
20 found at 1 position.
100 does not found in the list
20 does not found in the list
20 does not found in the list

Reference: List<T>.LastIndexOf Method




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.