List element removing

In this chapter you will learn:

  1. How to remove elements from List

Remove elements

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

using System;//from  ja v  a2s.co m
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"));


    }
}

Next chapter...

What you will learn in the next chapter:

  1. How to convert List to Array