List<T> 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 List<T> 類別的新執行個體。
多載
List<T>() |
初始化 List<T> 類別的新執行個體,這個執行個體為空白且具有預設的初始容量。 |
List<T>(IEnumerable<T>) |
初始化 List<T> 類別的新執行個體,其包含從指定之集合複製的項目,且具有容納複製之項目數目的足夠容量。 |
List<T>(Int32) |
為具有指定初始容量且為空的 List<T> 類別,初始化新的執行個體。 |
List<T>()
- 來源:
- List.cs
- 來源:
- List.cs
- 來源:
- List.cs
初始化 List<T> 類別的新執行個體,這個執行個體為空白且具有預設的初始容量。
public:
List();
public List ();
Public Sub New ()
範例
下列範例示範泛型類別的 List<T> 無參數建構函式。 無參數建構函式會建立具有預設容量的清單,如顯示 Capacity 屬性所示。
此範例會新增、插入和移除專案,其中顯示使用這些方法時容量的變更方式。
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
*)
備註
的 List<T> 容量是 可以保留的項目 List<T> 數目。 當元素新增至 List<T>時,重新配置內部數位會自動增加容量。
如果可以估計集合的大小,請使用 List<T>(Int32) 建構函式並指定初始容量,就不需要在將專案加入 至 List<T>時執行數個重設大小作業。
呼叫 方法或明確設定 Capacity 屬性,即可減少TrimExcess容量。 減少容量會重新配置記憶體,並複製 中的所有 List<T>元素。
此建構函式是 O (1) 作業。
適用於
List<T>(IEnumerable<T>)
- 來源:
- List.cs
- 來源:
- List.cs
- 來源:
- List.cs
初始化 List<T> 類別的新執行個體,其包含從指定之集合複製的項目,且具有容納複製之項目數目的足夠容量。
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))
參數
- collection
- IEnumerable<T>
將其項目複製到新清單的來源集合。
例外狀況
collection
為 null
。
範例
下列範例示範 List<T> 對範圍採取動作之 List<T> 類別的建構函式和各種方法。 字串陣列會建立並傳遞至建構函式,以數位列的專案填入清單。 Capacity接著會顯示 屬性,以顯示初始容量是保存輸入元素所需的確切容量。
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
備註
元素會以集合的欄位舉值讀取的順序複製到 List<T> 。
此建構函式是 O (n) 作業,其中 n 是中的 collection
項目數目。
另請參閱
適用於
List<T>(Int32)
- 來源:
- List.cs
- 來源:
- List.cs
- 來源:
- List.cs
為具有指定初始容量且為空的 List<T> 類別,初始化新的執行個體。
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)
參數
- capacity
- Int32
新清單一開始能夠儲存的項目數目。
例外狀況
capacity
小於 0。
範例
下列範例示範建 List<T>(Int32) 構函式。 List<T>系統會建立容量為 4 的字串,因為清單的最終大小已知為 4。 清單會填入四個字串,並使用 方法來建立 AsReadOnly 唯讀複本。
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
備註
的 List<T> 容量是 可以保留的項目 List<T> 數目。 當元素新增至 List<T>時,重新配置內部數位會自動增加容量。
如果可以估計集合的大小,則指定初始容量就不需要在將元素加入 至 List<T>時執行一些重設大小作業。
呼叫 方法或明確設定 Capacity 屬性,即可減少TrimExcess容量。 減少容量會重新配置記憶體,並複製 中的所有 List<T>元素。
此建構函式是 O (1) 作業。