Remove from Generic List in C#

Hi Friends,

In the below post we will learn about the methods used to remove elements from the generic list of C#. We have four methods as below:-

1. Remove(T item)

This method takes input of the specified type and remove the first occurrence of this item. This method can take null as input if the specified type( T) is reference type. This method return true if item is successfully removed and false if item is not found or it failed to remove that item.

2. RemoveAll(Predicate<T> match)

This method takes a predicate of the specified type as input and remove all the matching items. The return value of this method gives you the count of items it removed from the List.

3. RemoveAt(int index)

This method takes index( zero based) as input and try to remove that item if position is valid. This method throws ‘System.ArgumentOutOfRangeException’ if the index is not a valid position in the List.

4. RemoveRange(int index, int count)

This method takes index( zero based) as input and try to remove that item if position is valid to the given count as second parameter. This method throws ‘System.ArgumentOutOfRangeException’ if the index or count is not a valid position in the List.

Below is the sample program to demonstrate all above methods use.

using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace GenericListSample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                List marksList = new List();
                int[] marks = { 12, 14, 18, 15,18,13,16,74 };
                marksList.AddRange(marks);
                Debug.WriteLine("\nInitial List");
                foreach (var item in marksList)
                {
                    Debug.Write(item + "  ");
                }
                marksList.Remove(18);
                Debug.WriteLine("\nAfter using Remove(18)");
                foreach (var item in marksList)
                {
                    Debug.Write(item + "  ");
                }
                marksList.RemoveAll(item => (item / 2) > 9);
                Debug.WriteLine("\nAfter using RemoveAll(item => (item / 2) > 9)");
                foreach (var item in marksList)
                {
                    Debug.Write(item + "  ");
                }
                marksList.RemoveAt(0);
                Debug.WriteLine("\nAfter using RemoveAt(0)");
                foreach (var item in marksList)
                {
                    Debug.Write(item + "  ");
                }
                marksList.RemoveRange(0,2);
                Debug.WriteLine("\nAfter using RemoveRange(0,2)");
                foreach (var item in marksList)
                {
                    Debug.Write(item + "  ");
                }
            }
            catch (ArgumentNullException ex)
            {
                Debug.WriteLine(ex.Message);
            }
            catch(ArgumentOutOfRangeException ex)
            {
                Debug.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
}

You will see below output in output window of Visual studio.

Initial List
12 14 18 15 18 13 16 74
After using Remove(18)
12 14 15 18 13 16 74
After using RemoveAll(item => (item / 2) > 9)
12 14 15 18 13 16
After using RemoveAt(0)
14 15 18 13 16
After using RemoveRange(0,2)
18 13 16

Hope this helps you.:)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s