Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Collection<T>.RemoveItem Method

Removes the element at the specified index of the Collection<T>.

Namespace:  System.Collections.ObjectModel
Assembly:  mscorlib (in mscorlib.dll)

protected virtual void RemoveItem(
	int index
)

Parameters

index
Type: System.Int32
The zero-based index of the element to remove.

ExceptionCondition
ArgumentOutOfRangeException

index is less than zero.

-or-

index is equal to or greater than Count.

This method is an O(n) operation, where n is Count.

The following code example shows how to derive a collection class from a constructed type of the Collection<T> generic class, and how to override the protected InsertItem, RemoveItem, ClearItems, and SetItem methods to provide custom behavior for the Add, Insert, Remove, and Clear methods, and for setting the Item property.

The custom behavior provided by this example is a Changed notification event that is raised at the end of each of the protected methods. The Dinosaurs class inherits Collection<string> (Collection(Of String) in Visual Basic) and defines the Changed event, which uses a DinosaursChangedEventArgs class for the event information, and an enumeration to identify the kind of change.

The code example calls several properties and methods of Collection<T> to demonstrate the custom event.


using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Controls;

public class Dinosaurs : Collection<string>
{
    public event EventHandler<DinosaursChangedEventArgs> Changed;


    protected override void InsertItem(int index, string newItem)
    {
        base.InsertItem(index, newItem);

        EventHandler<DinosaursChangedEventArgs> temp = Changed;
        if (temp != null)
        {
            temp(this, new DinosaursChangedEventArgs(
                ChangeType.Added, newItem, null));
        }
    }

    protected override void SetItem(int index, string newItem)
    {
        string replaced = Items[index];
        base.SetItem(index, newItem);

        EventHandler<DinosaursChangedEventArgs> temp = Changed;
        if (temp != null)
        {
            temp(this, new DinosaursChangedEventArgs(
                ChangeType.Replaced, replaced, newItem));
        }
    }

    protected override void RemoveItem(int index)
    {
        string removedItem = Items[index];
        base.RemoveItem(index);

        EventHandler<DinosaursChangedEventArgs> temp = Changed;
        if (temp != null)
        {
            temp(this, new DinosaursChangedEventArgs(
                ChangeType.Removed, removedItem, null));
        }
    }

    protected override void ClearItems()
    {
        base.ClearItems();

        EventHandler<DinosaursChangedEventArgs> temp = Changed;
        if (temp != null)
        {
            temp(this, new DinosaursChangedEventArgs(
                ChangeType.Cleared, null, null));
        }
    }
}

// Event argument for the Changed event.
//
public class DinosaursChangedEventArgs : EventArgs
{
    public readonly string ChangedItem;
    public readonly ChangeType ChangeType;
    public readonly string ReplacedWith;

    public DinosaursChangedEventArgs(ChangeType change, string item,
        string replacement)
    {
        ChangeType = change;
        ChangedItem = item;
        ReplacedWith = replacement;
    }
}

public enum ChangeType
{
    Added,
    Removed,
    Replaced,
    Cleared
};

public class Example
{

    private static TextBlock _textblock;


    public static void Demo(TextBlock outputBlock)
    {

        Example._textblock = outputBlock;

        Dinosaurs dinosaurs = new Dinosaurs();

        dinosaurs.Changed += ChangedHandler;

        dinosaurs.Add("Psitticosaurus");
        dinosaurs.Add("Caudipteryx");
        dinosaurs.Add("Compsognathus");
        dinosaurs.Add("Muttaburrasaurus");

        Display(dinosaurs);

        outputBlock.Text += String.Format("\nIndexOf(\"Muttaburrasaurus\"): {0}\n",
            dinosaurs.IndexOf("Muttaburrasaurus"));

        outputBlock.Text += String.Format("\nContains(\"Caudipteryx\"): {0}\n",
            dinosaurs.Contains("Caudipteryx"));

        outputBlock.Text += String.Format("\nInsert(2, \"Nanotyrannus\")\n");
        dinosaurs.Insert(2, "Nanotyrannus");

        outputBlock.Text += String.Format("\ndinosaurs[2]: {0}\n", dinosaurs[2]);

        outputBlock.Text += "\ndinosaurs[2] = \"Microraptor\"" + "\n";
        dinosaurs[2] = "Microraptor";

        outputBlock.Text += "\nRemove(\"Microraptor\")\n";
        dinosaurs.Remove("Microraptor");

        outputBlock.Text += "\nRemoveAt(0)\n";
        dinosaurs.RemoveAt(0);

        Display(dinosaurs);
    }

    private static void Display(Collection<string> cs)
    {
        _textblock.Text += "\n";
        foreach (string item in cs)
        {
            _textblock.Text += item + "\n";
        }
    }

    private static void ChangedHandler(object source,
        DinosaursChangedEventArgs e)
    {

        if (e.ChangeType == ChangeType.Replaced)
        {
            _textblock.Text += String.Format("{0} was replaced with {1}\n", e.ChangedItem,
                e.ReplacedWith);
        }
        else if (e.ChangeType == ChangeType.Cleared)
        {
            _textblock.Text += "The dinosaur list was cleared." + "\n";
        }
        else
        {
            _textblock.Text += String.Format("{0} was {1}.\n", e.ChangedItem, e.ChangeType);
        }
    }
}


/* This code example produces the following output:

Psitticosaurus was Added.
Caudipteryx was Added.
Compsognathus was Added.
Muttaburrasaurus was Added.

Psitticosaurus
Caudipteryx
Compsognathus
Muttaburrasaurus

IndexOf("Muttaburrasaurus"): 3

Contains("Caudipteryx"): True

Insert(2, "Nanotyrannus")
Nanotyrannus was Added.

dinosaurs[2]: Nanotyrannus

dinosaurs[2] = "Microraptor"
Nanotyrannus was replaced with Microraptor

Remove("Microraptor")
Microraptor was Removed.

RemoveAt(0)
Psitticosaurus was Removed.

Caudipteryx
Compsognathus
Muttaburrasaurus
 */


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft