List.FindIndex() Method in C# with Examples (original) (raw)
Last Updated : 19 Jul, 2024
**List.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. 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:
- **FindIndex(Predicate)Method
- **FindIndex(Int32,Predicate)Method
- **FindIndex(Int32, Int32, Predicate) Method
FindIndex(Predicate) 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.
**Syntax: public int FindIndex (Predicate match);
**Parameter:
**match: It is the **Predicate 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.FindIndex (Predicate ) 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) 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 match);
**Parameter:
- **startIndex: It is the zero-based starting index of the search.
- **match: It is the **Predicate 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.
**Example:
csharp `
// C# program to demonstrate the use of // List.FindIndex (Int32, Predicate ) 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) 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 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 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 or **count is less than 0 or **startIndex and **count do not specify a valid section in the List.
**Example:
csharp `
// C# program to demonstrate the use of // List.FindIndex (Int32, Int32, // Predicate ) 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: