Add() method is used to insert a new Item in a List.
Let us say, we have a List that contains three names, Mohan, Kriti and Salim. And we want to insert a new name Mika at the end of the List.
We can use the Add() Method without any parameter to achieve the above.
using System.Collections.Generic;
public class MyApplication
{
public static void Main(string[] args)
{
var x = new List<string>(){"Mohan", "Kriti", "Salim"};
x.Add("Mika");
foreach (var data in x)
{
System.Console.WriteLine(data);
}
}
}
So, in the above code we have created a List and initialised to the variable x.
var x = new List<string>(){"Mohan", "Kriti", "Salim"};Below is how the values are positioned in the List,
-Method1.png)
Next, we have used the Add() function to add the new name Mika at the end of the List.
x.Add("Mika");-Method2.png)
And we get the below output,