C# Array - FindIndex() Method



The C# Array FindIndex() method returns the index of the first occurrence within an array or part of it after finding an element matching the position defined by the specified predicate.

Syntax

Following is the syntax of the C# Array FindIndex() method −

public static T? FindIndex<T> (T[] array, Predicate<T> match);
public static T? FindIndex<T> (T[], Int32, Predicate<T> match);

Parameters

This method accepts the following parameters −

  • array: The one dimensional zero based array to search for an index.
  • match: The predicate that defines the condition of the element to search for.

Return value

This method returns an index that matches the predicate if found; otherwise -1 will return.

Example 1: Find Index of First Even Number

This is the basic example of the FindIndex() method to display the index of first occurrence of even number of an array −

using System;
class Program {
   static void Main() {
      int[] Number = new int[] {1, 2, 3, 4, 5};
   
      // Find Index of first even number
      int evenN = Array.FindIndex(Number, num => num % 2 == 0);
   
      Console.WriteLine("Index of First Even number is: " + evenN);
   }
}

Output

Following is the output −

Index of First Even number is: 1

Example 2: Find first Index of a String

Let's create an example that display the index of the first string whose character starts with 'c' using the FindIndex() method −

using System;
class Program {
   static void Main() {
      string[] names = { "Dipak", "Rhaul", "Chhavi", "Charlie" };

      // FindIndex name that starts with 'C'
      int index_number = Array.FindIndex(names, name => name.StartsWith("C"));
      Console.WriteLine("Index of First name starting with 'C': " + index_number);
   }
}

Output

Following is the output −

Index of First name starting with 'C': 2

Example 3: What if No Match Found

Let's see another example of the FindIndex() method and how it handles the case where no index is found in the array −

using System;
class Program {
   static void Main() {
      int[] numbers = {1, 3, 5, 7};

      int index = Array.FindIndex(numbers, num => num % 2 == 0);
      Console.WriteLine("Index of first even number: " + index);
   }
}

Output

Following is the output −

Index of first even number: -1

Example 4: Find Index of a Custom Object Matching a Condition

In this example, we create a custom object that takes the person's name and age. We use the FindIndex() function to display index of first person who is older than 23 −

using System;
class Person {
   public string Name {
      get;
      set;
   }
   public int Age {
      get;
      set;
   }
}
class Program {
   static void Main() {
      Person[] people = {
         new Person {
            Name = "Dipak", Age = 25
         },
         new Person {
            Name = "Karan", Age = 30
         },
         new Person {
            Name = "Pankaj", Age = 22
         }
      };

      // Find Index of the first person older than 23
      int first_index = Array.FindIndex(people, person => person.Age > 23);

      Console.WriteLine("Index of First person older than 23: " + first_index);
   }
}

Output

Following is the output −

Index of First person older than 23: 0
csharp_array_class.htm
Advertisements