42
1
using System;
2
using System.Collections.Generic;
3
4
public class Program
5
{
6
public static void Main()
7
{
8
// You can add things to the list on initialisation
9
List<string> stringList = new List<string>()
10
{
11
"first string"
12
};
13
14
stringList.Add("third string");
15
16
// adds an item to the list at the specificed index.
17
// Since lists are 0 based, this adds the item to the second position
18
// Insert will throw an error if you try and insert after the last value
19
stringList.Insert(1, "second string");
20
21
// The range commands add a list of values, rather than 1 at a time
22
// This would add "some string" and "second string" to the list
23
stringList.AddRange(stringList);
24
25
stringList.InsertRange(4, stringList);
26
27
// This only removes the first occurrence
28
stringList.Remove("third string");
29
30
// You need to use remove all to remove all occurrences
31
stringList.RemoveAll(stringToRemove => stringToRemove == "second string");
32
33
// This is the equivalent of insertion for removal
34
// This would remove the first item in the list
35
stringList.RemoveAt(0);
36
37
// Remove everything
38
stringList.Clear();
39
40
stringList.ForEach(x => Console.WriteLine(x));
41
}
42
}
Cached Result