How to get Index of an item in a C# List

The IndexOf method returns the first index of an item if found in the List.

List<string> AuthorList = new List<string>();
AuthorList.Add("Mahesh Chand");
AuthorList.Add("Praveen Kumar");
AuthorList.Add("Raj Kumar");
AuthorList.Add("Nipun Tomar");
AuthorList.Add("Mahesh Chand");
AuthorList.Add("Dinesh Beniwal");
            
// Contains - Check if an item is in the list  
if (AuthorList.Contains("Mahesh Chand")) 
{
    Console.WriteLine("Author found!");
}        

// Find an item and replace it with new item
int idx = AuthorList.IndexOf("Nipun Tomar");
if (idx >= 0)
{
    AuthorList[idx] = "New Author";
}
Console.WriteLine("\nIndexOf ");
foreach (var author in AuthorList)
{
    Console.WriteLine(author);
}



Up Next
    Ebook Download
    View all
    Learn
    View all