Export (0) Print
Expand All

List(Of T) Constructor

Initializes a new instance of the List(Of T) class that is empty and has the default initial capacity.

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

'Declaration
Public Sub New
'Usage
Dim instance As New List()

The capacity of a List(Of T) is the number of elements that the List(Of T) can hold. As elements are added to a List(Of T), the capacity is automatically increased as required by reallocating the internal array.

If the size of the collection can be estimated, using the List(Of T)(Int32) constructor and specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the List(Of T).

The capacity can be decreased by calling the TrimExcess method or by setting the Capacity property explicitly. Decreasing the capacity reallocates memory and copies all the elements in the List(Of T).

This constructor is an O(1) operation.

The following code example demonstrates the default constructor of the List(Of T) generic class. The default constructor creates a list with the default capacity, as demonstrated by displaying the Capacity property.

The code example adds, inserts, and removes items, showing how the capacity changes as these methods are used.

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

        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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft