Let us say, we have a List that contains three names, Mohan, Kriti and Salim. And we want to replace the name Kriti with a new name Paul.
using System.Collections.Generic; public class MyApplication { public static void Main(string[] args) { var x = new List<string>(){"Mohan", "Kriti", "Salim"}; x[1] = "Paul"; foreach (var data in x) { System.Console.WriteLine(data); } } }
So, in the above code we have created a List x.
var x = new List<string>(){"Mohan", "Kriti", "Salim"};
Now, let us see, how the values are positioned in the List
Now, if we see the above diagram, Kriti resides at position/index 1. So, what we do is, just replace the position/index 1 (i.e. x[1]) with the new name Paul.
x[1] = "Paul"
And we get the below output,