Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
List FindLastIndex() Method in C# | Set -1
Next article icon

List.FindIndex() Method in C# with Examples

Last Updated : 19 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 that 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:

  1. FindIndex(Predicate<T>)Method
  2. FindIndex(Int32,Predicate<T>)Method
  3. 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 the match, if found. If not found it returns -1. Exception: This method will give ArgumentNullException if the 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)); 	} } 

Output
'C' starts at index 0 'P' starts at index 4

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));     } } 

Output
Search for 'C' starts at index 1 Search for 'J' starts at index 2

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)); 		 	} } 

Output
Search for 'C' starts at index 0 Search for 'J' starts at index 3

Reference:

  • https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.findindex?view=netframework-4.7.2


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

S

SoumikMondal
Improve
Article Tags :
  • C#
  • Technical Scripter
  • CSharp-Generic-List
  • CSharp-Generic-Namespace
  • CSharp-method

Similar Reads

  • List FindLastIndex() Method in C# | Set -2
    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: FindLastIndex(Predicate<T>) M
    5 min read
  • List FindLastIndex() Method in C# | Set -1
    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: FindLastIndex(Predicate<T>) M
    3 min read
  • Array.GetValue() Method in C# with Examples | Set – 4
    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(
    5 min read
  • 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.GetVa
    4 min read
  • Array.GetValue() Method in C# with Examples | Set – 3
    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(
    5 min read
  • Array.GetValue() Method in C# with Examples | Set – 2
    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.GetVa
    3 min read
  • AbstractList subList() method in Java with Examples
    The subList() method of java.util.AbstractList class is used to return a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural
    3 min read
  • Array.BinarySearch(Array, Int32, Int32, Object) Method with examples in C#
    Array.BinarySearch(Array, int32, int32, Object) Method is used to search a range of elements in a one-dimensional sorted array for a value, using the IComparable interface implemented by each element of the array and by the specified value. It searches only in a specified boundary that the user defi
    4 min read
  • C# | Finding the index of last element in the array
    GetUpperBound() Method is used to find the index of the last element of the specified dimension in the array. Syntax: public int GetUpperBound (int dimension); Here, dimension is a zero-based dimension of the array whose upper bound needs to be determined. Return Value:The return type of this method
    2 min read
  • SortedList ContainsKey() Method in C# With Examples
    Given a SortedList object, now our task is to check whether the given SortedList object contains the specific key or not. So to do this task we use ContainsKey() method. This method is used to determine whether a SortedList object contains a specific key or not. It will return true if the key is fou
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
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
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences