List<T> 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 List<T> 类的新实例。
重载
List<T>() |
初始化 List<T> 类的新实例,该实例为空并且具有默认初始容量。 |
List<T>(IEnumerable<T>) |
初始化 List<T> 类的新实例,该实例包含从指定集合复制的元素并且具有足够的容量来容纳所复制的元素。 |
List<T>(Int32) |
初始化 List<T> 类的新实例,该实例为空并且具有指定的初始容量。 |
List<T>()
- Source:
- List.cs
- Source:
- List.cs
- Source:
- 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>时执行大量调整大小操作。
可以通过调用 TrimExcess 方法或显式设置 Capacity 属性来降低容量。 降低容量会重新分配内存并复制 中的所有 List<T>元素。
此构造函数是一个 O (1) 操作。
适用于
List<T>(IEnumerable<T>)
- Source:
- List.cs
- Source:
- List.cs
- Source:
- 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)
- Source:
- List.cs
- Source:
- List.cs
- Source:
- 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>时执行大量调整大小操作。
可以通过调用 TrimExcess 方法或显式设置 Capacity 属性来降低容量。 降低容量会重新分配内存并复制 中的所有 List<T>元素。
此构造函数是一个 O (1) 操作。