Add
MethodΒΆ
Adds object to the end of the List. Often used when initializing a new list object.
listName.Add(object);
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | List<string> planets = new List<string>();
planets.Add("Mercury");
planets.Add("Venus");
planets.Add("Earth");
planets.Add("Mars");
planets.Add("Jupiter");
planets.Add("Saturn");
planets.Add("Uranus");
planets.Add("Neptune");
foreach(string planet in planets)
{
Console.WriteLine(planet);
}
|
Output
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune
//Oops! forgot Pluto
planets.Add("Pluto");
foreach(string planet in planets)
{
Console.WriteLine(planet);
}
Output
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune
Pluto