C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Step 1: We create a List and add several strings to it. This List is used to demonstrate how Remove methods work.
Step 2: We call Remove to remove an element by its value. So the element with the value "bulldog" is removed.
Step 3: The example shows RemoveAt. It removes the element with index 1, which is the second dog, "otterhound".
C# program that uses Remove on List
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Step 1: create List.
List<string> dogs = new List<string>();
dogs.Add("maltese");
dogs.Add("otterhound");
dogs.Add("rottweiler");
dogs.Add("bulldog");
dogs.Add("whippet");
// Step 2: remove by value.
dogs.Remove("bulldog");
foreach (string dog in dogs)
{
Console.WriteLine(dog);
}
Console.WriteLine();
// Step 3: remove by index.
dogs.RemoveAt(1);
foreach (string dog in dogs)
{
Console.WriteLine(dog);
}
}
}
Output
maltese
otterhound
rottweiler
whippet
maltese
rottweiler
whippet
Next: We remove all elements except the last two. The code is robust because it uses Math.Max to avoid negative parameters.
Math.Max, MinC# program that uses RemoveRange method
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);
list.Add(5);
// Remove all except last 2
int remove = Math.Max(0, list.Count - 2);
list.RemoveRange(0, remove);
foreach (int i in list)
{
Console.Write(i);
}
}
}
Output
45
C# program that uses RemoveRange on first elements
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);
list.Add(5);
// Remove first 2 elements
int remove = Math.Min(list.Count, 2);
list.RemoveRange(0, remove);
foreach (int i in list)
{
Console.Write(i);
}
}
}
Output
345
Example: In this program we specify that a List be instantiated with three strings: "dot", "net" and "Codex".
Next: We remove the string at index 1, which is the second string. The List now contains only two strings: "dot" and "Codex".
VarString LiteralC# program that uses RemoveAt
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var list = new List<string>();
list.Add("dot");
list.Add("net");
list.Add("Codex");
list.RemoveAt(1);
foreach (string element in list)
Console.WriteLine(element);
}
}
Output
dot
Codex
Note: RemoveAt is faster than Remove. It doesn't need to iterate through the number of elements equal to index.
RemoveAll: You can use RemoveAll to remove all elements in the List that match a certain predicate expression.
Tip: It is often easiest to use the lambda expression syntax with the RemoveAll method, which can reduce line count.
RemoveAllHowever: If you want to remove an element but eliminate copying, you could simply assign it to null or default(T).
NullDefaultAlso: The Insert method on List suffers from a similar performance drawback. It causes excessive element copying.
Insert