
RemoveAt removes one element. How is this method on the List type implemented in the .NET Framework? It requires an index argument. We look at both the usage of RemoveAt and its implementation.

In this program we specify that a List be instantiated with three strings: "dot", "net" and "perls". Next, we remove the string at index 1, which is the second string. The List now contains only two strings: "dot" and "perls".
Var Examples List Examples String Literal Foreach Loop ExamplesThis C# example program uses the RemoveAt method on the List type.
Program that uses RemoveAt [C#]
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var list = new List<string>();
list.Add("dot");
list.Add("net");
list.Add("perls");
list.RemoveAt(1);
foreach (string element in list)
Console.WriteLine(element);
}
}
Output
dot
perls
My reason for looking at the RemoveAt method was to determine its implementation. Unfortunately, when you call RemoveAt, all the element following the index you remove will be copied and shifted forward. This is not very efficient. If you want to remove an element but eliminate copying, you could simply assign it to null or default(T). Your other code would then have to handle null elements.
Null Tips Default
We tested the RemoveAt method on the List type and also looked at its internals. RemoveAt will cause all following elements in the list to be copied to new locations in memory. If you call RemoveAt frequently on a large list, this can lead to degenerate performance. Instead, using a LinkedList or a scheme where null or default elements are treated specially is recommended.
List Remove Methods LinkedList Programs Collections