A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/list-findlastindex-method-in-c-sharp-set-1/ below:

List FindLastIndex() Method in C# | Set -1

List FindLastIndex() Method in C# | Set -1

Last Updated : 11 Jul, 2025

This method is used to search for an element which matches the conditions defined by a specified predicate and returns the zero-based index of the last occurrence within the List<T> or a portion of it. There are 3 methods in the overload list of this method:

Here, we will discuss only the first method i.e.

FindLastIndex(Predicate<T>) List<T>.FindLastIndex(Predicate<T>) Method

searches for an element that matches the conditions defined by the specified predicate and returns the zero-based index of the last occurrence within the entire

List<T>

.

Syntax:
public int FindLastIndex (Predicate <T> match);

Here, the

match

is the Predicate <T> delegate that defines the conditions of the element to search for.

Return Value:

If the element is found then it returns the zero-based index of type int or Int32 of the last element that matches a specified condition by the parameter "match". And if not found then it returns "-1".

Exception:

This method will throw

ArgumentNullException

if the

match

is null.

Example 1:

In this example, creating a List named "PC" that contains some element. Our task is to find an element named "Computer" and prints its index.

CSharp
// C# Program to illustrate the 
// FindLastIndex(Predicate<T>)
// Method
using System;
using System.Collections.Generic;

class GFG {

    // Main Method
    public static void Main()
    {

        // List creation
        // List name is "PC"
        List<string> PC = new List<string>();

        // elements in the List
        PC.Add("mouse");
        PC.Add("keyboard");
        PC.Add("laptop");
        PC.Add("Computer");

        // using the method
        int indx = PC.FindLastIndex(predi);

        Console.WriteLine(indx);
    }

    // Conditional method
    private static bool predi(string g)
    {

        if (g == "Computer") {
            return true;
        }
        else {
            return false;
        }
    }
}
Example 2:

This example is the extended form of the previous example. In this example, we use an XML file and search an item and prints the index of that item. If the item is not found then prints "-1" and if found then prints the index. The item is\ "GeeksForGeeks".

CSharp
// C# Program to illustrate the 
// FindLastIndex(Predicate<T>)
// Method
using System;
using System.Collections.Generic;
using System.Linq;

class GFG {

    // here List<T> contains the
    // object "gfg" using
    // data from a sample XML file
    // "geeks" is the List name
    private static List<gfg> geeks = new List<gfg>();

    // Main Method
    public static void Main()
    {

        // if the item is found 
        // then it prints the index
        // if not found prints "-1"
        int x = geeks.FindLastIndex(FindGFG);
        Console.WriteLine(x); 
    }

    // conditional method
    private static bool FindGFG(gfg g)
    {

        if (g.G == "GeeksForGeeks")
        {
            return true;
        }
        else {
            return false;
        }
    }
}

public class gfg {

    public string G
    {
        get;
        set;
    }
}
Note: Reference:

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4