List<T> Constructors
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Initialiseert een nieuw exemplaar van de List<T> klasse.
Overloads
| Name | Description |
|---|---|
| List<T>() |
Initialiseert een nieuw exemplaar van de List<T> klasse die leeg is en heeft de standaardinitiële capaciteit. |
| List<T>(IEnumerable<T>) |
Initialiseert een nieuw exemplaar van de List<T> klasse die elementen bevat die zijn gekopieerd uit de opgegeven verzameling en heeft voldoende capaciteit voor het aantal gekopieerde elementen. |
| List<T>(Int32) |
Initialiseert een nieuw exemplaar van de List<T> klasse die leeg is en heeft de opgegeven initiële capaciteit. |
List<T>()
Initialiseert een nieuw exemplaar van de List<T> klasse die leeg is en heeft de standaardinitiële capaciteit.
public:
List();
public List();
Public Sub New ()
Voorbeelden
In het volgende voorbeeld ziet u de parameterloze constructor van de List<T> algemene klasse. De constructor zonder parameter maakt een lijst met de standaardcapaciteit, zoals wordt gedemonstreerd door de Capacity eigenschap weer te geven.
In het voorbeeld worden items toegevoegd, ingevoegd en verwijderd, waarin wordt getoond hoe de capaciteit verandert terwijl deze methoden worden gebruikt.
List<string> dinosaurs = new List<string>();
Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity);
dinosaurs.Add("Tyrannosaurus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Mamenchisaurus");
dinosaurs.Add("Deinonychus");
dinosaurs.Add("Compsognathus");
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity);
Console.WriteLine("Count: {0}", dinosaurs.Count);
Console.WriteLine("\nContains(\"Deinonychus\"): {0}",
dinosaurs.Contains("Deinonychus"));
Console.WriteLine("\nInsert(2, \"Compsognathus\")");
dinosaurs.Insert(2, "Compsognathus");
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
// Shows accessing the list using the Item property.
Console.WriteLine("\ndinosaurs[3]: {0}", dinosaurs[3]);
Console.WriteLine("\nRemove(\"Compsognathus\")");
dinosaurs.Remove("Compsognathus");
Console.WriteLine();
foreach(string dinosaur in dinosaurs)
{
Console.WriteLine(dinosaur);
}
dinosaurs.TrimExcess();
Console.WriteLine("\nTrimExcess()");
Console.WriteLine("Capacity: {0}", dinosaurs.Capacity);
Console.WriteLine("Count: {0}", dinosaurs.Count);
dinosaurs.Clear();
Console.WriteLine("\nClear()");
Console.WriteLine("Capacity: {0}", dinosaurs.Capacity);
Console.WriteLine("Count: {0}", dinosaurs.Count);
/* 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
*/
Imports System.Collections.Generic
Partial Public Class Program
Public Shared Sub ShowPlanets()
Dim planets As New List(Of String)
Console.WriteLine(vbLf & "Capacity: {0}", planets.Capacity)
planets.Add("Mercury")
planets.Add("Venus")
planets.Add("Earth")
planets.Add("Mars")
planets.Add("Jupiter")
Console.WriteLine()
For Each planet As String In planets
Console.WriteLine(planet)
Next
Console.WriteLine(vbLf & "Capacity: {0}", planets.Capacity)
Console.WriteLine("Count: {0}", planets.Count)
Console.WriteLine(vbLf & "Contains(""Mars""): {0}", _
planets.Contains("Mars"))
Console.WriteLine(vbLf & "Insert(2, ""Saturn"")")
planets.Insert(2, "Saturn")
Console.WriteLine()
For Each planet As String In planets
Console.WriteLine(planet)
Next
' Shows how to access the list using the Item property.
Console.WriteLine(vbLf & "planets(3): {0}", planets(3))
Console.WriteLine(vbLf & "Remove(""Jupiter"")")
planets.Remove("Jupiter")
Console.WriteLine()
For Each planet As String In planets
Console.WriteLine(planet)
Next
planets.TrimExcess()
Console.WriteLine(vbLf & "TrimExcess()")
Console.WriteLine("Capacity: {0}", planets.Capacity)
Console.WriteLine("Count: {0}", planets.Count)
planets.Clear()
Console.WriteLine(vbLf & "Clear()")
Console.WriteLine("Capacity: {0}", planets.Capacity)
Console.WriteLine("Count: {0}", planets.Count)
End Sub
End Class
' This code example produces the following output:
'
' Capacity: 0
'
' Mercury
' Venus
' Earth
' Mars
' Jupiter
'
' Capacity: 8
' Count: 5
'
' Contains("Mars"): True
'
' Insert(2, "Saturn")
'
' Mercury
' Venus
' Saturn
' Earth
' Mars
' Jupiter
'
' planets(3): Earth
'
' Remove("Jupiter")
'
' Mercury
' Venus
' Saturn
' Earth
' Mars
'
' TrimExcess()
' Capacity: 5
' Count: 5
'
' Clear()
' Capacity: 5
' Count: 0
[<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
*)
Opmerkingen
De capaciteit van een List<T> is het aantal elementen dat de List<T> kan bevatten. Als er elementen aan een List<T>worden toegevoegd, wordt de capaciteit automatisch verhoogd zoals vereist door de interne matrix opnieuw te verplaatsen.
Als de grootte van de verzameling kan worden geschat, met behulp van de List<T>(Int32) constructor en het opgeven van de initiële capaciteit, hoeft u niet meer een aantal groottebewerkingen uit te voeren terwijl er elementen aan de List<T>verzameling worden toegevoegd.
De capaciteit kan worden verminderd door de TrimExcess methode aan te roepen of door de Capacity eigenschap expliciet in te stellen. Als u de capaciteit verlaagt, wordt het geheugen opnieuw verplaatst en worden alle elementen in de List<T>.
Deze constructor is een O(1)-bewerking.
Van toepassing op
List<T>(IEnumerable<T>)
Initialiseert een nieuw exemplaar van de List<T> klasse die elementen bevat die zijn gekopieerd uit de opgegeven verzameling en heeft voldoende capaciteit voor het aantal gekopieerde elementen.
public:
List(System::Collections::Generic::IEnumerable<T> ^ collection);
public List(System.Collections.Generic.IEnumerable<T> collection);
new System.Collections.Generic.List<'T> : seq<'T> -> System.Collections.Generic.List<'T>
Public Sub New (collection As IEnumerable(Of T))
Parameters
- collection
- IEnumerable<T>
De verzameling waarvan de elementen naar de nieuwe lijst worden gekopieerd.
Uitzonderingen
collection is null.
Voorbeelden
In het volgende voorbeeld ziet u de List<T> constructor en verschillende methoden van de List<T> klasse die op bereiken reageren. Er wordt een matrix met tekenreeksen gemaakt en doorgegeven aan de constructor, waarbij de lijst wordt gevuld met de elementen van de matrix. De Capacity eigenschap wordt vervolgens weergegeven om aan te geven dat de initiële capaciteit precies is wat nodig is om de invoerelementen te bewaren.
using System;
using System.Collections.Generic;
string[] input = { "Apple",
"Banana",
"Orange" };
List<string> fruits = new List<string>(input);
Console.WriteLine("\nCapacity: {0}", fruits.Capacity);
Console.WriteLine();
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
Console.WriteLine("\nAddRange(fruits)");
fruits.AddRange(fruits);
Console.WriteLine();
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
Console.WriteLine("\nRemoveRange(2, 2)");
fruits.RemoveRange(2, 2);
Console.WriteLine();
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
input = new string[] { "Mango",
"Pineapple",
"Watermelon" };
Console.WriteLine("\nInsertRange(3, input)");
fruits.InsertRange(3, input);
Console.WriteLine();
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
Console.WriteLine("\noutput = fruits.GetRange(2, 3).ToArray()");
string[] output = fruits.GetRange(2, 3).ToArray();
Console.WriteLine();
foreach (string fruit in output)
{
Console.WriteLine(fruit);
}
/*
This code example produces the following output:
Capacity: 3
Apple
Banana
Orange
AddRange(fruits)
Apple
Banana
Orange
Apple
Banana
Orange
RemoveRange(2, 2)
Apple
Banana
Banana
Orange
InsertRange(3, input)
Apple
Banana
Banana
Mango
Pineapple
Watermelon
Orange
output = fruits.GetRange(2, 3).ToArray()
Banana
Mango
Pineapple
*/
Imports System.Collections.Generic
Partial Public Class Program
Public Shared Sub ShowFruits()
Dim input() As String = { "Apple", _
"Banana", _
"Orange" }
Dim fruits As New List(Of String)(input)
Console.WriteLine(vbLf & "Capacity: {0}", fruits.Capacity)
Console.WriteLine()
For Each fruit As String In fruits
Console.WriteLine(fruit)
Next
Console.WriteLine(vbLf & "AddRange(fruits)")
fruits.AddRange(fruits)
Console.WriteLine()
For Each fruit As String In fruits
Console.WriteLine(fruit)
Next
Console.WriteLine(vbLf & "RemoveRange(2, 2)")
fruits.RemoveRange(2, 2)
Console.WriteLine()
For Each fruit As String In fruits
Console.WriteLine(fruit)
Next
input = New String() { "Mango", _
"Pineapple", _
"Watermelon" }
Console.WriteLine(vbLf & "InsertRange(3, input)")
fruits.InsertRange(3, input)
Console.WriteLine()
For Each fruit As String In fruits
Console.WriteLine(fruit)
Next
Console.WriteLine(vbLf & "output = fruits.GetRange(2, 3).ToArray")
Dim output() As String = fruits.GetRange(2, 3).ToArray()
Console.WriteLine()
For Each fruit As String In output
Console.WriteLine(fruit)
Next
End Sub
End Class
' This code example produces the following output:
'
' Capacity: 3
'
' Apple
' Banana
' Orange
'
' AddRange(fruits)
'
' Apple
' Banana
' Orange
' Apple
' Banana
' Orange
'
' RemoveRange(2, 2)
'
' Apple
' Banana
' Banana
' Orange
'
' InsertRange(3, input)
'
' Apple
' Banana
' Banana
' Mango
' Pineapple
' Watermelon
' Orange
'
' output = fruits.GetRange(2, 3).ToArray
'
' Banana
' Mango
' Pineapple
Opmerkingen
De elementen worden in List<T> dezelfde volgorde gekopieerd als ze worden gelezen door de opsomming van de verzameling.
Deze constructor is een O(n)-bewerking, waarbij n het aantal elementen in collectionis.
Zie ook
Van toepassing op
List<T>(Int32)
Initialiseert een nieuw exemplaar van de List<T> klasse die leeg is en heeft de opgegeven initiële capaciteit.
public:
List(int capacity);
public List(int capacity);
new System.Collections.Generic.List<'T> : int -> System.Collections.Generic.List<'T>
Public Sub New (capacity As Integer)
Parameters
- capacity
- Int32
Het aantal elementen dat de nieuwe lijst in eerste instantie kan opslaan.
Uitzonderingen
capacity is kleiner dan 0.
Voorbeelden
In het volgende voorbeeld ziet u de List<T>(Int32) constructor. Er wordt een List<T> tekenreeks met een capaciteit van 4 gemaakt, omdat de uiteindelijke grootte van de lijst precies 4 is. De lijst wordt gevuld met vier tekenreeksen en er wordt een alleen-lezen kopie gemaakt met behulp van de AsReadOnly methode.
using System;
using System.Collections.Generic;
public partial class Program
{
public static void Main()
{
List<string> animals = new List<string>(4);
Console.WriteLine("\nCapacity: {0}", animals.Capacity);
animals.Add("Cat");
animals.Add("Dog");
animals.Add("Squirrel");
animals.Add("Wolf");
Console.WriteLine();
foreach (string animal in animals)
{
Console.WriteLine(animal);
}
Console.WriteLine("\nIList<string> roAnimals = animals.AsReadOnly()");
IList<string> roAnimals = animals.AsReadOnly();
Console.WriteLine("\nElements in the read-only IList:");
foreach (string animal in roAnimals)
{
Console.WriteLine(animal);
}
Console.WriteLine("\nanimals[2] = \"Lion\"");
animals[2] = "Lion";
Console.WriteLine("\nElements in the read-only IList:");
foreach (string animal in roAnimals)
{
Console.WriteLine(animal);
}
}
}
/*
This code example produces the following output:
Capacity: 4
Cat
Dog
Squirrel
Wolf
IList<string> roAnimals = animals.AsReadOnly()
Elements in the read-only IList:
Cat
Dog
Squirrel
Wolf
animals[2] = "Lion"
Elements in the read-only IList:
Cat
Dog
Lion
Wolf
*/
Imports System.Collections.Generic
Partial Public Class Program
Public Shared Sub Main()
Dim animals As New List(Of String)(4)
Console.WriteLine(vbLf & "Capacity: {0}", animals.Capacity)
animals.Add("Cat")
animals.Add("Dog")
animals.Add("Squirrel")
animals.Add("Wolf")
Console.WriteLine()
For Each animal As String In animals
Console.WriteLine(animal)
Next
Console.WriteLine(vbLf & _
"Dim roAnimals As IList(Of String) = animals.AsReadOnly")
Dim roAnimals As IList(Of String) = animals.AsReadOnly
Console.WriteLine(vbLf & "Elements in the read-only IList:")
For Each animal As String In roAnimals
Console.WriteLine(animal)
Next
Console.WriteLine(vbLf & "animals(2) = ""Lion""")
animals(2) = "Lion"
Console.WriteLine(vbLf & "Elements in the read-only IList:")
For Each animal As String In roAnimals
Console.WriteLine(animal)
Next
End Sub
End Class
' This code example produces the following output:
'
' Capacity: 4
'
' Cat
' Dog
' Squirrel
' Wolf
'
' Dim roAnimals As IList(Of String) = animals.AsReadOnly
'
' Elements in the read-only IList:
' Cat
' Dog
' Squirrel
' Wolf
'
' animals(2) = "Lion"
'
' Elements in the read-only IList:
' Cat
' Dog
' Lion
' Wolf
Opmerkingen
De capaciteit van een List<T> is het aantal elementen dat de List<T> kan bevatten. Als er elementen aan een List<T>worden toegevoegd, wordt de capaciteit automatisch verhoogd zoals vereist door de interne matrix opnieuw te verplaatsen.
Als de grootte van de verzameling kan worden geschat, hoeft u bij het opgeven van de initiële capaciteit niet meer een aantal groottebewerkingen uit te voeren terwijl er elementen aan de List<T>verzameling worden toegevoegd.
De capaciteit kan worden verminderd door de TrimExcess methode aan te roepen of door de Capacity eigenschap expliciet in te stellen. Als u de capaciteit verlaagt, wordt het geheugen opnieuw verplaatst en worden alle elementen in de List<T>.
Deze constructor is een O(1)-bewerking.
Caution
Als capacity afkomstig is van gebruikersinvoer, geeft u de voorkeur aan het gebruik van de constructor zonder parameters en kunt u de grootte van de verzameling wijzigen als elementen worden toegevoegd. Als u een door de gebruiker opgegeven waarde moet gebruiken, kunt u deze vastzetten op een redelijke limiet (bijvoorbeeld Math.Clamp(untrustedValue, 0, 20)) of controleren of het aantal elementen overeenkomt met de opgegeven waarde.