How to replace list item in best way

Use Lambda to find the index in the List and use this index to replace the list item.

List<string> listOfStrings = new List<string> { "abc", "123", "ghi" };

int index = listOfStrings.FindIndex(s => s == "123");

if (index != -1)
    listOfStrings[index] =  "def";

Leave a Comment