This documentation is archived and is not being maintained.

Collection.ClearItems Method

Removes all elements from the Collection.

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

Protected Overridable Sub ClearItems

protected void ClearItems ()
protected function ClearItems ()
Not applicable.

Count is set to zero, and references to other objects from elements of the collection are also released.

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

Notes to Inheritors: Derived classes can override this method to change the behavior of the Clear method.

The following code example shows how to derive a collection class from a constructed type of the Collection 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 to demonstrate the custom event.

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Public Class Dinosaurs
    Inherits Collection(Of String)

    Public Event Changed As EventHandler(Of DinosaursChangedEventArgs)

    Protected Overrides Sub InsertItem( _
        ByVal index As Integer, ByVal newItem As String)

        MyBase.InsertItem(index, newItem)

        RaiseEvent Changed(Me, New DinosaursChangedEventArgs( _
            ChangeType.Added, newItem, Nothing))
    End Sub

    Protected Overrides Sub SetItem(ByVal index As Integer, _
        ByVal newItem As String)

        Dim replaced As String = Items(index)
        MyBase.SetItem(index, newItem)

        RaiseEvent Changed(Me, New DinosaursChangedEventArgs( _
            ChangeType.Replaced, replaced, newItem))
    End Sub

    Protected Overrides Sub RemoveItem(ByVal index As Integer)

        Dim removedItem As String = Items(index)

        RaiseEvent Changed(Me, New DinosaursChangedEventArgs( _
            ChangeType.Removed, removedItem, Nothing))
    End Sub

    Protected Overrides Sub ClearItems()

        RaiseEvent Changed(Me, New DinosaursChangedEventArgs( _
            ChangeType.Cleared, Nothing, Nothing))
    End Sub

End Class

' Event argument for the Changed event.
Public Class DinosaursChangedEventArgs
    Inherits EventArgs

    Public ReadOnly ChangedItem As String
    Public ReadOnly ChangeType As ChangeType
    Public ReadOnly ReplacedWith As String

    Public Sub New(ByVal change As ChangeType, ByVal item As String, _
        ByVal replacement As String)

        ChangeType = change
        ChangedItem = item
        ReplacedWith = replacement
    End Sub
End Class

Public Enum ChangeType
End Enum

Public Class Demo
    Public Shared Sub Main() 

        Dim dinosaurs As New Dinosaurs

        AddHandler dinosaurs.Changed, AddressOf ChangedHandler


        Console.WriteLine(vbLf & "IndexOf(""Muttaburrasaurus""): {0}", _

        Console.WriteLine(vbLf & "Contains(""Caudipteryx""): {0}", _

        Console.WriteLine(vbLf & "Insert(2, ""Nanotyrannus"")")
        dinosaurs.Insert(2, "Nanotyrannus")

        Console.WriteLine(vbLf & "dinosaurs(2): {0}", dinosaurs(2))

        Console.WriteLine(vbLf & "dinosaurs(2) = ""Microraptor""")
        dinosaurs(2) = "Microraptor"

        Console.WriteLine(vbLf & "Remove(""Microraptor"")")

        Console.WriteLine(vbLf & "RemoveAt(0)")

    End Sub
    Private Shared Sub Display(ByVal cs As Collection(Of String)) 
        For Each item As String In cs
        Next item
    End Sub

    Private Shared Sub ChangedHandler(ByVal source As Object, _
        ByVal e As DinosaursChangedEventArgs)

        If e.ChangeType = ChangeType.Replaced Then
            Console.WriteLine("{0} was replaced with {1}", _
                e.ChangedItem, e.ReplacedWith)

        ElseIf e.ChangeType = ChangeType.Cleared Then
            Console.WriteLine("The dinosaur list was cleared.")

            Console.WriteLine("{0} was {1}.", _
                e.ChangedItem, e.ChangeType)
        End If
    End Sub

End Class

' This code example produces the following output:
'Psitticosaurus was Added.
'Caudipteryx was Added.
'Compsognathus was Added.
'Muttaburrasaurus was Added.
'IndexOf("Muttaburrasaurus"): 3
'Contains("Caudipteryx"): True
'Insert(2, "Nanotyrannus")
'Nanotyrannus was Added.
'dinosaurs(2): Nanotyrannus
'dinosaurs(2) = "Microraptor"
'Nanotyrannus was replaced with Microraptor
'Microraptor was Removed.
'Psitticosaurus was Removed.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0