Export (0) Print
Expand All

List(T) Constructor

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

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

new : unit -> List

The capacity of a List(T) is the number of elements that the List(T) can hold. As elements are added to a List(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(T)(Int32) constructor and specifying the initial capacity eliminates the need to perform a number of resizing operations while adding elements to the List(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(T).

This constructor is an O(1) operation.

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

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


[<EntryPoint>]
let main argv = 
    // We refer to System.Collections.Generic.List<'T> by its type  
    // abbreviation ResizeArray<'T> to avoid conflict with the List module.     
    // Note: In F# code, F# linked lists are usually preferred over 
    // ResizeArray<'T> when an extendable collection is required. 
    let dinosaurs = ResizeArray<_>()

    // Write out the dinosaurs in the ResizeArray. 
    let printDinosaurs() =
        printfn ""
        dinosaurs |> Seq.iter (fun p -> printfn "%O" p) 


    printfn "\nCapacity: %i" dinosaurs.Capacity

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

    printDinosaurs()

    printfn "\nCapacity: %i" dinosaurs.Capacity
    printfn "Count: %i" dinosaurs.Count

    printfn "\nContains(\"Deinonychus\"): %b" (dinosaurs.Contains("Deinonychus"))

    printfn "\nInsert(2, \"Compsognathus\")"
    dinosaurs.Insert(2, "Compsognathus")

    printDinosaurs()

    // Shows accessing the list using the Item property.
    printfn "\ndinosaurs[3]: %s" dinosaurs.[3]

    printfn "\nRemove(\"Compsognathus\")"
    dinosaurs.Remove("Compsognathus") |> ignore

    printDinosaurs()

    dinosaurs.TrimExcess()
    printfn "\nTrimExcess()"
    printfn "Capacity: %i" dinosaurs.Capacity
    printfn "Count: %i" dinosaurs.Count

    dinosaurs.Clear()
    printfn "\nClear()"
    printfn "Capacity: %i" dinosaurs.Capacity
    printfn "Count: %i" dinosaurs.Count

    0 // return an integer exit code 

    (* 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, 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 Silverlight 8.1, Windows Phone Silverlight 8

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.

Show:
© 2014 Microsoft