List<T> | Finding Index Operator | FindIndex

FindIndex method is one of the methods of System.Collections.Generic. FindIndex method returns the index of the first item that meets the given condition. If there is no item that meets the given condition, it returns -1. FindIndex has its overload methods that takes parameters: Predicate delegate, int startIndex and int count. Usage of this methos is so simple. Let’s consider the following examples to better understanding of how it works.

 

FindIndex(Predicate<T> match)

Returns the index of the first item that meets the given condition. If there is no item that meets this condition returns -1.

using System;
using System.Collections.Generic;

namespace ZeroToCSharp {
    class Program {
        static void Main(string[] args) {
            List<string> books = new List<string>();
            books.Add("Clean Code");
            books.Add("Clean Architecture");
            books.Add("Refactoring");

            var index = books.FindIndex(x => x.Contains("r"));

            //To print output
            Console.WriteLine(string.Concat("The index of the item that contains r letter is: ", index, " and It is ", books[index]));
            Console.ReadKey();
        }
    }
}

 

Output:

The index of the item that contains r letter is: 1 and It is Clean Architecture

 

 

FindIndex(int startIndex, Predicate<T> match)

Searches for the index by starting startIndex and returns the index of the first item that meets the given condition. If there is no item that meets this condition returns -1.

 

using System;
using System.Collections.Generic;

namespace ZeroToCSharp {
    class Program {
        static void Main(string[] args) {
            List<string> books = new List<string>();
            books.Add("Clean Code");
            books.Add("Clean Architecture");
            books.Add("Refactoring");
            books.Add("Gang of Four Design Patterns");


            var index = books.FindIndex(2, x => x.Contains("r"));

            //To print output
            Console.WriteLine(string.Concat("The index of the item that contains r letter is: ", index, " and It is ", books[index]));
            Console.ReadKey();
        }
    }
}

 

Output:

The index of the item that contains r letter is: 2 and It is Refactoring

 

FindIndex(int startIndex, int count, Predicate<T> match)

Searches for the index for count items by starting startIndex and returns the index of the first item that meets the given condition. If there is no item that meets this condition returns -1.

 

using System;
using System.Collections.Generic;

namespace ZeroToCSharp {
    class Program {
        static void Main(string[] args) {
            List<string> books = new List<string>();
            books.Add("Clean Code");
            books.Add("Clean Architecture");
            books.Add("Refactoring");
            books.Add("Gang of Four Design Patterns");

            var index = books.FindIndex(2, 2, x => x.Contains("Gang"));

            //To print output
            Console.WriteLine(string.Concat("The index of the item that contains Gang word is: ", index, " and It is ", books[index]));
            Console.ReadKey();
        }
    }
}

 

Output:

The index of the item that contains Gang word is: 3 and It is Gang of Four Design Patterns
0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x