C# List RemoveAt

Description

List RemoveAt removes the element at the specified index of the List .

Syntax


public void RemoveAt(
  int index
)

Parameters

List.RemoveAt has the following parameters.

  • index - The zero-based index of the element to remove.

Example

The following code shows different ways to remove elements from a List.


using System;/*from   w  w w . j av a2  s . com*/
using System.Collections;
using System.Collections.Generic;

class Sample
{
    public static void Main()
    {
        //Create a List of Strings
        //String-typed list
        List<string> words = new List<string>();  

        words.Add("A");
        words.Add("B");
        words.Add("C");
        words.Add("D");
        words.Add("E");
        words.Add("F");

        words.Remove("A");
        words.RemoveAt(3);  // Remove the 4th element 
        words.RemoveRange (0, 2); // Remove first 2 elements

        // Remove all strings starting in 'n':
        words.RemoveAll(s => s.StartsWith("n"));


    }
}




















Home »
  C# Tutorial »
    System.Collections.Generic »




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack