Export (0) Print
Expand All
1 out of 2 rated this helpful - Rate this topic

List(Of T).Clear Method

Removes all elements from the List(Of T).

Namespace:  System.Collections.Generic
Assembly:  mscorlib (in mscorlib.dll)
'Declaration
Public Sub Clear

Implements

ICollection(Of T).Clear
IList.Clear

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

Capacity remains unchanged. To reset the capacity of the List(Of T), call the TrimExcess method or set the Capacity property directly. Decreasing the capacity reallocates memory and copies all the elements in the List(Of T). Trimming an empty List(Of T) sets the capacity of the List(Of T) to the default capacity.

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

The following example demonstrates the Clear method and various other properties and methods of the List(Of T) generic class. The Clear method is used at the end of the program, to remove all items from the list, and the Capacity and Count properties are then displayed.

Imports System
Imports System.Collections.Generic

Public Class Example

    Public Shared Sub Main()
        Dim dinosaurs As New List(Of String)

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

        dinosaurs.Add("Tyrannosaurus")
        dinosaurs.Add("Amargasaurus")
        dinosaurs.Add("Mamenchisaurus")
        dinosaurs.Add("Deinonychus")
        dinosaurs.Add("Compsognathus")

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

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

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

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

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next 
        ' Shows how to access the list using the Item property.
        Console.WriteLine(vbLf & "dinosaurs(3): {0}", dinosaurs(3))
        Console.WriteLine(vbLf & "Remove(""Compsognathus"")")
        dinosaurs.Remove("Compsognathus")

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        dinosaurs.TrimExcess()
        Console.WriteLine(vbLf & "TrimExcess()")
        Console.WriteLine("Capacity: {0}", dinosaurs.Capacity)
        Console.WriteLine("Count: {0}", dinosaurs.Count)

        dinosaurs.Clear()
        Console.WriteLine(vbLf & "Clear()")
        Console.WriteLine("Capacity: {0}", dinosaurs.Capacity)
        Console.WriteLine("Count: {0}", dinosaurs.Count)
    End Sub 
End Class 

' This code example produces the following output: 

'Capacity: 0 

'Tyrannosaurus 
'Amargasaurus 
'Mamenchisaurus 
'Deinonychus 
'Compsognathus 

'Capacity: 8 
'Count: 5 

'Contains("Deinonychus"): True 

'Insert(2, "Compsognathus") 

'Tyrannosaurus 
'Amargasaurus 
'Compsognathus 
'Mamenchisaurus 
'Deinonychus 
'Compsognathus 

'dinosaurs(3): Mamenchisaurus 

'Remove("Compsognathus") 

'Tyrannosaurus 
'Amargasaurus 
'Mamenchisaurus 
'Deinonychus 
'Compsognathus 

'TrimExcess() 
'Capacity: 5 
'Count: 5 

'Clear() 
'Capacity: 5 
'Count: 0

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.