List<T> Konstruktor
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menginisialisasi instans baru kelas List<T>.
Overload
List<T>() |
Menginisialisasi instans List<T> baru kelas yang kosong dan memiliki kapasitas awal default. |
List<T>(IEnumerable<T>) |
Menginisialisasi instans List<T> baru kelas yang berisi elemen yang disalin dari koleksi yang ditentukan dan memiliki kapasitas yang cukup untuk mengakomodasi jumlah elemen yang disalin. |
List<T>(Int32) |
Menginisialisasi instans baru kelas List<T> yang kosong dan memiliki kapasitas awal yang ditentukan. |
List<T>()
- Sumber:
- List.cs
- Sumber:
- List.cs
- Sumber:
- List.cs
Menginisialisasi instans List<T> baru kelas yang kosong dan memiliki kapasitas awal default.
public:
List();
public List ();
Public Sub New ()
Contoh
Contoh berikut menunjukkan konstruktor tanpa parameter dari List<T> kelas generik. Konstruktor tanpa parameter membuat daftar dengan kapasitas default, seperti yang ditunjukkan dengan menampilkan Capacity properti .
Contoh menambahkan, menyisipkan, dan menghapus item, memperlihatkan bagaimana kapasitas berubah saat metode ini digunakan.
using namespace System;
using namespace System::Collections::Generic;
void main()
{
List<String^>^ dinosaurs = gcnew 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();
for each(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();
for each(String^ dinosaur in dinosaurs )
{
Console::WriteLine(dinosaur);
}
Console::WriteLine("\ndinosaurs[3]: {0}", dinosaurs[3]);
Console::WriteLine("\nRemove(\"Compsognathus\")");
dinosaurs->Remove("Compsognathus");
Console::WriteLine();
for each(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
*/
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
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
' Shows how to access the list using the Item property.
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
[<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
*)
Keterangan
Kapasitas a List<T> adalah jumlah elemen yang dapat dipegang List<T> . Saat elemen ditambahkan ke List<T>, kapasitas secara otomatis ditingkatkan sesuai kebutuhan dengan merealokasi array internal.
Jika ukuran koleksi dapat diperkirakan, menggunakan List<T>(Int32) konstruktor dan menentukan kapasitas awal menghilangkan kebutuhan untuk melakukan sejumlah operasi pengubahan ukuran sambil menambahkan elemen ke List<T>.
Kapasitas dapat dikurangi dengan memanggil TrimExcess metode atau dengan mengatur Capacity properti secara eksplisit. Mengurangi kapasitas mengalokasikan memori dan menyalin semua elemen di List<T>.
Konstruktor ini adalah operasi O(1).
Berlaku untuk
List<T>(IEnumerable<T>)
- Sumber:
- List.cs
- Sumber:
- List.cs
- Sumber:
- List.cs
Menginisialisasi instans List<T> baru kelas yang berisi elemen yang disalin dari koleksi yang ditentukan dan memiliki kapasitas yang cukup untuk mengakomodasi jumlah elemen yang disalin.
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))
Parameter
- collection
- IEnumerable<T>
Koleksi yang elemennya disalin ke daftar baru.
Pengecualian
collection
adalah null
.
Contoh
Contoh berikut menunjukkan List<T> konstruktor dan berbagai metode List<T> kelas yang bertindak berdasarkan rentang. Array string dibuat dan diteruskan ke konstruktor, mengisi daftar dengan elemen array. Properti Capacity kemudian ditampilkan, untuk menunjukkan bahwa kapasitas awal adalah apa yang diperlukan untuk menahan elemen input.
using namespace System;
using namespace System::Collections::Generic;
void main()
{
array<String^>^ input = { "Brachiosaurus",
"Amargasaurus",
"Mamenchisaurus" };
List<String^>^ dinosaurs =
gcnew List<String^>((IEnumerable<String^>^) input);
Console::WriteLine("\nCapacity: {0}", dinosaurs->Capacity);
Console::WriteLine();
for each(String^ dinosaur in dinosaurs )
{
Console::WriteLine(dinosaur);
}
Console::WriteLine("\nAddRange(dinosaurs)");
dinosaurs->AddRange(dinosaurs);
Console::WriteLine();
for each(String^ dinosaur in dinosaurs )
{
Console::WriteLine(dinosaur);
}
Console::WriteLine("\nRemoveRange(2, 2)");
dinosaurs->RemoveRange(2, 2);
Console::WriteLine();
for each(String^ dinosaur in dinosaurs )
{
Console::WriteLine(dinosaur);
}
input = gcnew array<String^> { "Tyrannosaurus",
"Deinonychus",
"Velociraptor"};
Console::WriteLine("\nInsertRange(3, (IEnumerable<String^>^) input)");
dinosaurs->InsertRange(3, (IEnumerable<String^>^) input);
Console::WriteLine();
for each(String^ dinosaur in dinosaurs )
{
Console::WriteLine(dinosaur);
}
Console::WriteLine("\noutput = dinosaurs->GetRange(2, 3)->ToArray()");
array<String^>^ output = dinosaurs->GetRange(2, 3)->ToArray();
Console::WriteLine();
for each(String^ dinosaur in output )
{
Console::WriteLine(dinosaur);
}
}
/* This code example produces the following output:
Capacity: 3
Brachiosaurus
Amargasaurus
Mamenchisaurus
AddRange(dinosaurs)
Brachiosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Amargasaurus
Mamenchisaurus
RemoveRange(2, 2)
Brachiosaurus
Amargasaurus
Amargasaurus
Mamenchisaurus
InsertRange(3, (IEnumerable<String^>^) input)
Brachiosaurus
Amargasaurus
Amargasaurus
Tyrannosaurus
Deinonychus
Velociraptor
Mamenchisaurus
output = dinosaurs->GetRange(2, 3)->ToArray()
Amargasaurus
Tyrannosaurus
Deinonychus
*/
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
string[] input = { "Brachiosaurus",
"Amargasaurus",
"Mamenchisaurus" };
List<string> dinosaurs = new List<string>(input);
Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity);
Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nAddRange(dinosaurs)");
dinosaurs.AddRange(dinosaurs);
Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\nRemoveRange(2, 2)");
dinosaurs.RemoveRange(2, 2);
Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
}
input = new string[] { "Tyrannosaurus",
"Deinonychus",
"Velociraptor"};
Console.WriteLine("\nInsertRange(3, input)");
dinosaurs.InsertRange(3, input);
Console.WriteLine();
foreach( string dinosaur in dinosaurs )
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\noutput = dinosaurs.GetRange(2, 3).ToArray()");
string[] output = dinosaurs.GetRange(2, 3).ToArray();
Console.WriteLine();
foreach( string dinosaur in output )
{
Console.WriteLine(dinosaur);
}
}
}
/* This code example produces the following output:
Capacity: 3
Brachiosaurus
Amargasaurus
Mamenchisaurus
AddRange(dinosaurs)
Brachiosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Amargasaurus
Mamenchisaurus
RemoveRange(2, 2)
Brachiosaurus
Amargasaurus
Amargasaurus
Mamenchisaurus
InsertRange(3, input)
Brachiosaurus
Amargasaurus
Amargasaurus
Tyrannosaurus
Deinonychus
Velociraptor
Mamenchisaurus
output = dinosaurs.GetRange(2, 3).ToArray()
Amargasaurus
Tyrannosaurus
Deinonychus
*/
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
Dim input() As String = { "Brachiosaurus", _
"Amargasaurus", _
"Mamenchisaurus" }
Dim dinosaurs As New List(Of String)(input)
Console.WriteLine(vbLf & "Capacity: {0}", dinosaurs.Capacity)
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & "AddRange(dinosaurs)")
dinosaurs.AddRange(dinosaurs)
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & "RemoveRange(2, 2)")
dinosaurs.RemoveRange(2, 2)
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
input = New String() { "Tyrannosaurus", _
"Deinonychus", _
"Velociraptor" }
Console.WriteLine(vbLf & "InsertRange(3, input)")
dinosaurs.InsertRange(3, input)
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & "output = dinosaurs.GetRange(2, 3).ToArray")
Dim output() As String = dinosaurs.GetRange(2, 3).ToArray()
Console.WriteLine()
For Each dinosaur As String In output
Console.WriteLine(dinosaur)
Next
End Sub
End Class
' This code example produces the following output:
'
'Capacity: 3
'
'Brachiosaurus
'Amargasaurus
'Mamenchisaurus
'
'AddRange(dinosaurs)
'
'Brachiosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Amargasaurus
'Mamenchisaurus
'
'RemoveRange(2, 2)
'
'Brachiosaurus
'Amargasaurus
'Amargasaurus
'Mamenchisaurus
'
'InsertRange(3, input)
'
'Brachiosaurus
'Amargasaurus
'Amargasaurus
'Tyrannosaurus
'Deinonychus
'Velociraptor
'Mamenchisaurus
'
'output = dinosaurs.GetRange(2, 3).ToArray
'
'Amargasaurus
'Tyrannosaurus
'Deinonychus
Keterangan
Elemen disalin ke List<T> dalam urutan yang sama yang dibaca oleh enumerator koleksi.
Konstruktor ini adalah operasi O(n), di mana n adalah jumlah elemen dalam collection
.
Lihat juga
Berlaku untuk
List<T>(Int32)
- Sumber:
- List.cs
- Sumber:
- List.cs
- Sumber:
- List.cs
Menginisialisasi instans baru kelas List<T> yang kosong dan memiliki kapasitas awal yang ditentukan.
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)
Parameter
- capacity
- Int32
Jumlah elemen yang awalnya dapat disimpan oleh daftar baru.
Pengecualian
capacity
kurang dari 0.
Contoh
Contoh berikut menunjukkan List<T>(Int32) konstruktor. List<T> String dengan kapasitas 4 dibuat, karena ukuran akhir daftar diketahui tepat 4. Daftar diisi dengan empat string, dan salinan baca-saja dibuat dengan menggunakan AsReadOnly metode .
using namespace System;
using namespace System::Collections::Generic;
void main()
{
List<String^>^ dinosaurs = gcnew List<String^>(4);
Console::WriteLine("\nCapacity: {0}", dinosaurs->Capacity);
dinosaurs->Add("Tyrannosaurus");
dinosaurs->Add("Amargasaurus");
dinosaurs->Add("Mamenchisaurus");
dinosaurs->Add("Deinonychus");
Console::WriteLine();
for each(String^ dinosaur in dinosaurs)
{
Console::WriteLine(dinosaur);
}
Console::WriteLine("\nIList<String^>^ roDinosaurs = dinosaurs->AsReadOnly()");
IList<String^>^ roDinosaurs = dinosaurs->AsReadOnly();
Console::WriteLine("\nElements in the read-only IList:");
for each(String^ dinosaur in roDinosaurs)
{
Console::WriteLine(dinosaur);
}
Console::WriteLine("\ndinosaurs[2] = \"Coelophysis\"");
dinosaurs[2] = "Coelophysis";
Console::WriteLine("\nElements in the read-only IList:");
for each(String^ dinosaur in roDinosaurs)
{
Console::WriteLine(dinosaur);
}
}
/* This code example produces the following output:
Capacity: 4
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Deinonychus
IList<String^>^ roDinosaurs = dinosaurs->AsReadOnly()
Elements in the read-only IList:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Deinonychus
dinosaurs[2] = "Coelophysis"
Elements in the read-only IList:
Tyrannosaurus
Amargasaurus
Coelophysis
Deinonychus
*/
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
List<string> dinosaurs = new List<string>(4);
Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity);
dinosaurs.Add("Tyrannosaurus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Mamenchisaurus");
dinosaurs.Add("Deinonychus");
Console.WriteLine();
foreach(string s in dinosaurs)
{
Console.WriteLine(s);
}
Console.WriteLine("\nIList<string> roDinosaurs = dinosaurs.AsReadOnly()");
IList<string> roDinosaurs = dinosaurs.AsReadOnly();
Console.WriteLine("\nElements in the read-only IList:");
foreach(string dinosaur in roDinosaurs)
{
Console.WriteLine(dinosaur);
}
Console.WriteLine("\ndinosaurs[2] = \"Coelophysis\"");
dinosaurs[2] = "Coelophysis";
Console.WriteLine("\nElements in the read-only IList:");
foreach(string dinosaur in roDinosaurs)
{
Console.WriteLine(dinosaur);
}
}
}
/* This code example produces the following output:
Capacity: 4
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Deinonychus
IList<string> roDinosaurs = dinosaurs.AsReadOnly()
Elements in the read-only IList:
Tyrannosaurus
Amargasaurus
Mamenchisaurus
Deinonychus
dinosaurs[2] = "Coelophysis"
Elements in the read-only IList:
Tyrannosaurus
Amargasaurus
Coelophysis
Deinonychus
*/
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
Dim dinosaurs As New List(Of String)(4)
Console.WriteLine(vbLf & "Capacity: {0}", dinosaurs.Capacity)
dinosaurs.Add("Tyrannosaurus")
dinosaurs.Add("Amargasaurus")
dinosaurs.Add("Mamenchisaurus")
dinosaurs.Add("Deinonychus")
Console.WriteLine()
For Each dinosaur As String In dinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & _
"Dim roDinosaurs As IList(Of String) = dinosaurs.AsReadOnly")
Dim roDinosaurs As IList(Of String) = dinosaurs.AsReadOnly
Console.WriteLine(vbLf & "Elements in the read-only IList:")
For Each dinosaur As String In roDinosaurs
Console.WriteLine(dinosaur)
Next
Console.WriteLine(vbLf & "dinosaurs(2) = ""Coelophysis""")
dinosaurs(2) = "Coelophysis"
Console.WriteLine(vbLf & "Elements in the read-only IList:")
For Each dinosaur As String In roDinosaurs
Console.WriteLine(dinosaur)
Next
End Sub
End Class
' This code example produces the following output:
'
'Capacity: 4
'
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Deinonychus
'
'Dim roDinosaurs As IList(Of String) = dinosaurs.AsReadOnly
'
'Elements in the read-only IList:
'Tyrannosaurus
'Amargasaurus
'Mamenchisaurus
'Deinonychus
'
'dinosaurs(2) = "Coelophysis"
'
'Elements in the read-only IList:
'Tyrannosaurus
'Amargasaurus
'Coelophysis
'Deinonychus
Keterangan
Kapasitas a List<T> adalah jumlah elemen yang dapat dipegang List<T> . Saat elemen ditambahkan ke List<T>, kapasitas secara otomatis ditingkatkan sesuai kebutuhan dengan merealokasi array internal.
Jika ukuran koleksi dapat diperkirakan, menentukan kapasitas awal menghilangkan kebutuhan untuk melakukan sejumlah operasi pengubahan ukuran sambil menambahkan elemen ke List<T>.
Kapasitas dapat dikurangi dengan memanggil TrimExcess metode atau dengan mengatur Capacity properti secara eksplisit. Mengurangi kapasitas mengalokasikan memori dan menyalin semua elemen di List<T>.
Konstruktor ini adalah operasi O(1).