C# List RemoveRange

Description

List RemoveRange removes a range of elements from the List .

Syntax


public void RemoveRange(
  int index,
  int count
)

Parameters

  • index - The zero-based starting index of the range of elements to remove.
  • count - The number of elements to remove.

Example

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


using System;/*  w ww .  j  a  v a  2  s. c om*/
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