Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

List(T).Count Property

Gets the number of elements contained in the List(T).

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

abstract Count : int with get 
override Count : int with get

Property Value

Type: System.Int32
The number of elements contained in the List(T).

Implements

ICollection(T).Count
ICollection.Count
IReadOnlyCollection(T).Count

Capacity is the number of elements that the List(T) can store before resizing is required. Count is the number of elements that are actually in the List(T).

Capacity is always greater than or equal to Count. If Count exceeds Capacity while adding elements, the capacity is increased by automatically reallocating the internal array before copying the old elements and adding the new elements.

Retrieving the value of this property is an O(1) operation.

The following example demonstrates how to check the capacity and count of a List(T) that contains a simple business object, and illustrates using the TrimExcess method to remove extra capacity.

No code example is currently available or this language may not be supported.

The following example shows the value of the Count property at various points in the life of a list. After the list has been created and populated and its elements displayed, the Capacity and Count properties are displayed. These properties are displayed again after the TrimExcess method has been called, and again after the contents of the list are cleared.


[<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.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft