添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

List<T>.FindIndex Method is used to search for an element that matches the conditions defined by a specified predicate and returns the index of the first occurrence within the List<T>. If an item which matches the conditions is not found then this method will return -1. There are 3 methods in the overload list of this method as follows:

  • FindIndex(Predicate<T>)Method
  • FindIndex(Int32,Predicate<T>)Method
  • FindIndex(Int32, Int32, Predicate<T>) Method
  • FindIndex(Predicate<T>) Method

    This method is used to search for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the entire List<T>.

    Syntax: public int FindIndex (Predicate<T> match); Parameter: match: It is the Predicate<T> delegate that defines the conditions of the element to search for.

    Return Value: It returns the index of the first occurrence of an element that matches the conditions defined by match , if found. If not found it returns -1. Exception: This method will give ArgumentNullException if match is null. Example:

    csharp

    // C# program to demonstrate the use of
    // List<T>.FindIndex (Predicate <T>) method
    using System;
    using System.Collections.Generic;
    class GFG1 : IComparable {
    public String gg
    get ;
    set ;
    public int CompareTo(Object o)
    GFG1 e = o as GFG1;
    if (e == null )
    throw new ArgumentException("Not valid object ");
    return gg.CompareTo(e.gg);
    class GFG2 {
    String s;
    public GFG2(String ss)
    s = ss;
    public bool StartsWith(GFG1 e)
    return e.gg.StartsWith(s, StringComparison.InvariantCultureIgnoreCase);
    // Driver Class
    class GFG3 {
    // Main Method
    public static void Main()
    var e = new List<GFG1>();
    // List elements
    e.AddRange( new GFG1[] {
    new GFG1{
    gg = "c",
    new GFG1{
    gg = "c++",
    new GFG1{
    gg = "java",
    new GFG1{
    gg = "C#",
    new GFG1{
    gg = "Python",
    new GFG1{
    gg = "Perl",
    e.Sort();
    // sorts the list
    var es = new GFG2("C");
    Console.WriteLine(" 'C' starts at index "
    + e.FindIndex(es.StartsWith));
    es = new GFG2("P");
    Console.WriteLine(" 'P' starts at index " +
    e.FindIndex(es.StartsWith));

    FindIndex(Int32, Predicate<T>) Method

    This method searches for an element which matches the conditions defined by the specified predicate and returns the index of the first occurrence within the range of elements in the List which extends from the specified index to the last element.

    Syntax: public int FindIndex (int startIndex, Predicate<T> match); Parameter: startIndex: It is the zero-based starting index of the search. match: It is the Predicate<T> delegate that defines the conditions of the element to search for.

    Return Value: This method will return the index of the first occurrence of an element that matches the conditions defined by “match” , if found. If not found it returns -1. Exceptions:

  • ArgumentNullException: If the match is null.
  • ArgumentOutOfRangeException: If the startIndex is outside the range of valid indexes for the List<T>.
  • Example:

    csharp

    // C# program to demonstrate the use of
    // List<T>.FindIndex (Int32, Predicate <T>) method
    using System;
    using System.Collections.Generic;
    class GFG1 : IComparable {
    public String gg
    get ;
    set ;
    public int CompareTo(Object o)
    GFG1 e = o as GFG1;
    return gg.CompareTo(e.gg);
    class GFG2 {
    String s;
    public GFG2(String ss)
    s = ss;
    public bool StartsWith(GFG1 e)
    return e.gg.StartsWith(s, StringComparison.InvariantCultureIgnoreCase);
    // Driver Class
    class GFG3 {
    // Main Method
    public static void Main()
    var e = new List<GFG1>();
    // List elements
    e.AddRange( new GFG1[] {
    new GFG1{
    gg = "c",
    new GFG1{
    gg = "Python",
    new GFG1{
    gg = "Java",
    new GFG1{
    gg = "C#",
    new GFG1{
    gg = "Java",
    new GFG1{
    gg = "Perl",
    // sorts the list
    e.Sort();
    var es = new GFG2("C");
    // Search for c start from index 1
    Console.WriteLine("Search for 'C' starts at index "
    + e.FindIndex(1, es.StartsWith));
    es = new GFG2("J");
    // search for j starts from index 2
    Console.WriteLine("Search for 'J' starts at index "
    + e.FindIndex(2, es.StartsWith));

    FindIndex(Int32, Int32, Predicate<T>) Method

    This method searches for an element which matches the conditions defined by the specified predicate and returns the zero-based index of the first occurrence within the range of elements in the List that starts at the specified index and contains the specified number of elements.

    Syntax: public int FindIndex (int startIndex, int count, Predicate<T> match); Parameters: startIndex: It is the zero-based starting index of the search. count: It is the number of elements in the section to search. match: It is the Predicate<T> delegate that defines the conditions of the element to search for.

    Return Value: This method will return the index of the first occurrence of an element that matches the conditions defined by “match” , if found. If not found it returns -1. Exceptions:

  • ArgumentNullException: If the match is null.
  • ArgumentOutOfRangeException: If the startIndex is outside the range of valid indexes for the List<T> or count is less than 0 or startIndex and count do not specify a valid section in the List<T>.
  • Example:

    csharp

    // C# program to demonstrate the use of
    // List<T>.FindIndex (Int32, Int32,
    // Predicate <T>) method
    using System;
    using System.Collections.Generic;
    class GFG1 : IComparable {
    public String gg
    get ;
    set ;
    public int CompareTo(Object o)
    GFG1 e = o as GFG1;
    return gg.CompareTo(e.gg);
    class GFG2 {
    String s;
    public GFG2(String ss)
    s = ss;
    public bool StartsWith(GFG1 e)
    return e.gg.StartsWith(s, StringComparison.InvariantCultureIgnoreCase);
    // Driver Class
    class GFG3 {
    // Main Method
    public static void Main()
    var e = new List<GFG1>();
    // List elements
    e.AddRange( new GFG1[] {
    new GFG1{
    gg = "c",
    new GFG1{
    gg = "Python",
    new GFG1{
    gg = "C++",
    new GFG1{
    gg = "Java",
    new GFG1{
    gg = "C#",
    new GFG1{
    gg = "Perl",
    // sorts the list
    e.Sort();
    var es = new GFG2("C");
    // search for c start from index 1
    // count is 2 here
    Console.WriteLine("Search for 'C' starts at index "
    + e.FindIndex(0, 2, es.StartsWith));
    es = new GFG2("J");
    // search for j starts from index 2
    // count is 4 here
    Console.WriteLine("Search for 'J' starts at index "
    + e.FindIndex(2, 4, es.StartsWith));
    How to sort a list in C# | List.Sort() Method Set -1
    List&lt;T&gt;.Sort() Method is used to sort the elements or a portion of the elements in the List&lt;T&gt; using either the specified or default IComparer&lt;T&gt; implementation or a provided Comparison&lt;T&gt; delegate to compare list elements. There are total 4 methods in the overload list of this method as follows: Sort(IComparer&lt;T&gt;) Sor
    How to sort a list in C# | List.Sort() Method Set -2
    List&lt;T&gt;.Sort() Method is used to sort the elements or a portion of the elements in the List&lt;T&gt; using either the specified or default IComparer&lt;T&gt; implementation or a provided Comparison&lt;T&gt; delegate to compare list elements. There are total 4 methods in the overload list of this method as follows: Sort(IComparer&lt;T&gt;) Sor
    C# Program to Find the List of Students whose Name Starts with 'S' using where() Method of List Collection using LINQ
    LINQ is known as Language Integrated Query and it is introduced in .NET 3.5. It gives the ability to .NET languages to generate queries to retrieve data from the data source. It removes the mismatch between programming languages and databases and the syntax used to create a query is the same no matter which type of data source is used. In this arti
    C# Program to Find the List of Students whose Name Contains 4 Characters Using Where() Method of List Collection using LINQ
    LINQ is known as Language Integrated Query and it is introduced in .NET 3.5. It gives the ability to .NET languages to generate queries to retrieve data from the data source. It removes the mismatch between programming languages and databases and the syntax used to create a query is the same no matter which type of data source is used. In this arti
    Array.GetValue() Method in C# with Examples | Set - 1
    Array.GetValue() Method in C# is used to gets the value of the specified element in the current Array. There are total 8 methods in the overload list of this method which are as follows: Array.GetValue(Int32, Int32) Array.GetValue(Int64, Int64) Array.GetValue(Int32) Array.GetValue(Int64) Array.GetValue(Int32, Int32, Int32) Array.GetValue(Int64, Int
    File.GetLastWriteTimeUtc() Method in C# with Examples
    File.GetLastWriteTimeUtc(String) is an inbuilt File class method which is used to return the date and time, in coordinated universal time (UTC), that the specified file or directory was last written to.Syntax: public static DateTime GetLastWriteTimeUtc (string path); Parameter: This function accepts a parameter which is illustrated below: path: Thi
    MathF.Sin() Method in C# with Examples
    MathF.Sin(Single) is an inbuilt MathF class method which returns the sine of a given float value argument(specified angle). Syntax: public static float Sin (float x); Here, x is the angle(measured in radian) whose sine is to be returned and the type of this parameter is System.Single. Return Value: This method will return the sine of x of type Syst
    Double.CompareTo Method in C# with Examples
    Double.CompareTo Method is used to compare the current instance to a specified object or Double object. It will return an integer which shows whether the value of the current instance is greater than, less than or equal to the value of the specified object or Double object. There are 2 methods in the overload list of this method as follows: Compare
    UInt16.GetHashCode Method in C# with Examples
    UInt16.GetHashCode method is used to get the HashCode for the current UInt16 instance. Syntax: public override int GetHashCode (); Return Value: This method returns a 32-bit signed integer hash code. Below programs illustrate the use of the above discussed-method: Example 1: // C# program to illustrate the // UInt16.GetHashCode() Method using Syste
    Int64.CompareTo Method in C# with Examples
    Int64.CompareTo Method is used to compare the current instance to a specified object or Int64 and returns a sign of their relative values. There are 2 methods in the overload list of this method as follows: CompareTo(Int64) Method CompareTo(Object) Method Int64.CompareTo(Int64) Method This method is used to compare the current instance to a specifi
    We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Got It !
    Please go through our recently updated Improvement Guidelines before submitting any improvements.
    This article is being improved by another user right now. You can suggest the changes for now and it will be under the article's discussion tab.
    You will be notified via email once the article is available for improvement. Thank you for your valuable feedback!
    Please go through our recently updated Improvement Guidelines before submitting any improvements.
    Suggest Changes
    Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.