List<'T>.Add Method ('T)
Adds an object to the end of the List<'T>.
Assembly: mscorlib (in mscorlib.dll)
List<'T> accepts null as a valid value for reference types and allows duplicate elements.
If Count already equals Capacity, the capacity of the List<'T> is increased by automatically reallocating the internal array, and the existing elements are copied to the new array before the new element is added.
If Count is less than Capacity, this method is an O(1) operation. If the capacity needs to be increased to accommodate the new element, this method becomes an O(n) operation, where n is Count.
The following example demonstrates how to add, remove, and insert a simple business object in a List<'T>.
// Simple business object. A PartId is used to identify the type of part // but the part name can change. [<CustomEquality; NoComparison>] type Part = { PartId : int ; mutable PartName : string } with override this.GetHashCode() = hash this.PartId override this.Equals(other) = match other with | :? Part as p -> this.PartId = p.PartId | _ -> false override this.ToString() = sprintf "ID: %i Name: %s" this.PartId this.PartName [<EntryPoint>] let main argv = // We refer to System.Collections.Generic.List<'T> by its type // abbreviation ResizeArray<'T> to avoid conflicts with the F# List module. // Note: In F# code, F# linked lists are usually preferred over // ResizeArray<'T> when an extendable collection is required. let parts = ResizeArray<_>() parts.Add({PartName = "crank arm" ; PartId = 1234}) parts.Add({PartName = "chain ring"; PartId = 1334 }) parts.Add({PartName = "regular seat"; PartId = 1434 }) parts.Add({PartName = "banana seat"; PartId = 1444 }) parts.Add({PartName = "cassette"; PartId = 1534 }) parts.Add({PartName = "shift lever"; PartId = 1634 }) // Write out the parts in the ResizeArray. This will call the overridden ToString method // in the Part type printfn "" parts |> Seq.iter (fun p -> printfn "%O" p) // Check the ResizeArray for part #1734. This calls the IEquatable.Equals method // of the Part type, which checks the PartId for equality. printfn "\nContains(\"1734\"): %b" (parts.Contains({PartId=1734; PartName=""})) // Insert a new item at position 2. printfn "\nInsert(2, \"1834\")" parts.Insert(2, { PartName = "brake lever"; PartId = 1834 }) // Write out all parts parts |> Seq.iter (fun p -> printfn "%O" p) printfn "\nParts[3]: %O" parts.[3] printfn "\nRemove(\"1534\")" // This will remove part 1534 even though the PartName is different, // because the Equals method only checks PartId for equality. // Since Remove returns true or false, we need to ignore the result parts.Remove({PartId=1534; PartName="cogs"}) |> ignore // Write out all parts printfn "" parts |> Seq.iter (fun p -> printfn "%O" p) printfn "\nRemoveAt(3)" // This will remove the part at index 3. parts.RemoveAt(3) // Write out all parts printfn "" parts |> Seq.iter (fun p -> printfn "%O" p) 0 // return an integer exit code
The following example demonstrates several properties and methods of the List<'T> generic class, including the Add method. The default constructor is used to create a list of strings with a capacity of 0. The Capacity property is displayed, and then the Add method is used to add several items. The items are listed, and the Capacity property is displayed again, along with the Count property, to show that the capacity has been increased as needed.
Other properties and methods are used to search for, insert, and remove elements from the list, and finally to clear the list.
[<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 *)
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1