List<T> 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示可由索引访问的强类型对象列表。 提供用于搜索、排序和操作列表的方法。
generic <typename T>
public ref class List : System::Collections::Generic::ICollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IList<T>, System::Collections::Generic::IReadOnlyCollection<T>, System::Collections::Generic::IReadOnlyList<T>, System::Collections::IList
generic <typename T>
public ref class List : System::Collections::Generic::ICollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IList<T>, System::Collections::IList
public class List<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IList<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.Generic.IReadOnlyList<T>, System.Collections.IList
[System.Serializable]
public class List<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IList<T>, System.Collections.IList
[System.Serializable]
public class List<T> : System.Collections.Generic.ICollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IList<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.Generic.IReadOnlyList<T>, System.Collections.IList
type List<'T> = class
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
interface IList<'T>
interface IReadOnlyCollection<'T>
interface IReadOnlyList<'T>
interface ICollection
interface IList
[<System.Serializable>]
type List<'T> = class
interface IList<'T>
interface ICollection<'T>
interface seq<'T>
interface IList
interface ICollection
interface IEnumerable
[<System.Serializable>]
type List<'T> = class
interface IList<'T>
interface ICollection<'T>
interface IList
interface ICollection
interface IReadOnlyList<'T>
interface IReadOnlyCollection<'T>
interface seq<'T>
interface IEnumerable
[<System.Serializable>]
type List<'T> = class
interface IList<'T>
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
interface IList
interface ICollection
interface IReadOnlyList<'T>
interface IReadOnlyCollection<'T>
type List<'T> = class
interface IList<'T>
interface ICollection<'T>
interface IReadOnlyList<'T>
interface IReadOnlyCollection<'T>
interface seq<'T>
interface IList
interface ICollection
interface IEnumerable
Public Class List(Of T)
Implements ICollection(Of T), IEnumerable(Of T), IList, IList(Of T), IReadOnlyCollection(Of T), IReadOnlyList(Of T)
Public Class List(Of T)
Implements ICollection(Of T), IEnumerable(Of T), IList, IList(Of T)
类型参数
- T
列表中的元素类型。
- 继承
-
List<T>
- 派生
- 属性
- 实现
示例
以下示例演示如何在 .. 中添加、删除和插入简单的业务对象 List<T>。
using System;
using System.Collections.Generic;
// Simple business object. A PartId is used to identify the type of part
// but the part name can change.
public class Part : IEquatable<Part>
{
public string PartName { get; set; }
public int PartId { get; set; }
public override string ToString()
{
return "ID: " + PartId + " Name: " + PartName;
}
public override bool Equals(object obj)
{
if (obj == null) return false;
Part objAsPart = obj as Part;
if (objAsPart == null) return false;
else return Equals(objAsPart);
}
public override int GetHashCode()
{
return PartId;
}
public bool Equals(Part other)
{
if (other == null) return false;
return (this.PartId.Equals(other.PartId));
}
// Should also override == and != operators.
}
public class Example
{
public static void Main()
{
// Create a list of parts.
List<Part> parts = new List<Part>();
// Add parts to the list.
parts.Add(new Part() { PartName = "crank arm", PartId = 1234 });
parts.Add(new Part() { PartName = "chain ring", PartId = 1334 });
parts.Add(new Part() { PartName = "regular seat", PartId = 1434 });
parts.Add(new Part() { PartName = "banana seat", PartId = 1444 });
parts.Add(new Part() { PartName = "cassette", PartId = 1534 });
parts.Add(new Part() { PartName = "shift lever", PartId = 1634 });
// Write out the parts in the list. This will call the overridden ToString method
// in the Part class.
Console.WriteLine();
foreach (Part aPart in parts)
{
Console.WriteLine(aPart);
}
// Check the list for part #1734. This calls the IEquatable.Equals method
// of the Part class, which checks the PartId for equality.
Console.WriteLine("\nContains(\"1734\"): {0}",
parts.Contains(new Part { PartId = 1734, PartName = "" }));
// Insert a new item at position 2.
Console.WriteLine("\nInsert(2, \"1834\")");
parts.Insert(2, new Part() { PartName = "brake lever", PartId = 1834 });
//Console.WriteLine();
foreach (Part aPart in parts)
{
Console.WriteLine(aPart);
}
Console.WriteLine("\nParts[3]: {0}", parts[3]);
Console.WriteLine("\nRemove(\"1534\")");
// This will remove part 1534 even though the PartName is different,
// because the Equals method only checks PartId for equality.
parts.Remove(new Part() { PartId = 1534, PartName = "cogs" });
Console.WriteLine();
foreach (Part aPart in parts)
{
Console.WriteLine(aPart);
}
Console.WriteLine("\nRemoveAt(3)");
// This will remove the part at index 3.
parts.RemoveAt(3);
Console.WriteLine();
foreach (Part aPart in parts)
{
Console.WriteLine(aPart);
}
/*
ID: 1234 Name: crank arm
ID: 1334 Name: chain ring
ID: 1434 Name: regular seat
ID: 1444 Name: banana seat
ID: 1534 Name: cassette
ID: 1634 Name: shift lever
Contains("1734"): False
Insert(2, "1834")
ID: 1234 Name: crank arm
ID: 1334 Name: chain ring
ID: 1834 Name: brake lever
ID: 1434 Name: regular seat
ID: 1444 Name: banana seat
ID: 1534 Name: cassette
ID: 1634 Name: shift lever
Parts[3]: ID: 1434 Name: regular seat
Remove("1534")
ID: 1234 Name: crank arm
ID: 1334 Name: chain ring
ID: 1834 Name: brake lever
ID: 1434 Name: regular seat
ID: 1444 Name: banana seat
ID: 1634 Name: shift lever
RemoveAt(3)
ID: 1234 Name: crank arm
ID: 1334 Name: chain ring
ID: 1834 Name: brake lever
ID: 1444 Name: banana seat
ID: 1634 Name: shift lever
*/
}
}
' Simple business object. A PartId is used to identify the type of part
' but the part name can change.
Public Class Part
Implements IEquatable(Of Part)
Public Property PartName() As String
Get
Return m_PartName
End Get
Set(value As String)
m_PartName = value
End Set
End Property
Private m_PartName As String
Public Property PartId() As Integer
Get
Return m_PartId
End Get
Set(value As Integer)
m_PartId = value
End Set
End Property
Private m_PartId As Integer
Public Overrides Function ToString() As String
Return "ID: " & PartId & " Name: " & PartName
End Function
Public Overrides Function Equals(obj As Object) As Boolean
If obj Is Nothing Then
Return False
End If
Dim objAsPart As Part = TryCast(obj, Part)
If objAsPart Is Nothing Then
Return False
Else
Return Equals(objAsPart)
End If
End Function
Public Overrides Function GetHashCode() As Integer
Return PartId
End Function
Public Overloads Function Equals(other As Part) As Boolean _
Implements IEquatable(Of Part).Equals
If other Is Nothing Then
Return False
End If
Return (Me.PartId.Equals(other.PartId))
End Function
' Should also override == and != operators.
End Class
Public Class Example
Public Shared Sub Main()
' Create a list of parts.
Dim parts As New List(Of Part)()
' Add parts to the list.
parts.Add(New Part() With {
.PartName = "crank arm",
.PartId = 1234
})
parts.Add(New Part() With {
.PartName = "chain ring",
.PartId = 1334
})
parts.Add(New Part() With {
.PartName = "regular seat",
.PartId = 1434
})
parts.Add(New Part() With {
.PartName = "banana seat",
.PartId = 1444
})
parts.Add(New Part() With {
.PartName = "cassette",
.PartId = 1534
})
parts.Add(New Part() With {
.PartName = "shift lever",
.PartId = 1634
})
' Write out the parts in the list. This will call the overridden ToString method
' in the Part class.
Console.WriteLine()
For Each aPart As Part In parts
Console.WriteLine(aPart)
Next
' Check the list for part #1734. This calls the IEquatable.Equals method
' of the Part class, which checks the PartId for equality.
Console.WriteLine(vbLf & "Contains(""1734""): {0}", parts.Contains(New Part() With {
.PartId = 1734,
.PartName = ""
}))
' Insert a new item at position 2.
Console.WriteLine(vbLf & "Insert(2, ""1834"")")
parts.Insert(2, New Part() With {
.PartName = "brake lever",
.PartId = 1834
})
'Console.WriteLine();
For Each aPart As Part In parts
Console.WriteLine(aPart)
Next
Console.WriteLine(vbLf & "Parts[3]: {0}", parts(3))
Console.WriteLine(vbLf & "Remove(""1534"")")
' This will remove part 1534 even though the PartName is different,
' because the Equals method only checks PartId for equality.
parts.Remove(New Part() With {
.PartId = 1534,
.PartName = "cogs"
})
Console.WriteLine()
For Each aPart As Part In parts
Console.WriteLine(aPart)
Next
Console.WriteLine(vbLf & "RemoveAt(3)")
' This will remove part at index 3.
parts.RemoveAt(3)
Console.WriteLine()
For Each aPart As Part In parts
Console.WriteLine(aPart)
Next
End Sub
'
' This example code produces the following output:
' ID: 1234 Name: crank arm
' ID: 1334 Name: chain ring
' ID: 1434 Name: regular seat
' ID: 1444 Name: banana seat
' ID: 1534 Name: cassette
' ID: 1634 Name: shift lever
'
' Contains("1734"): False
'
' Insert(2, "1834")
' ID: 1234 Name: crank arm
' ID: 1334 Name: chain ring
' ID: 1834 Name: brake lever
' ID: 1434 Name: regular seat
' ID: 1444 Name: banana seat
' ID: 1534 Name: cassette
' ID: 1634 Name: shift lever
'
' Parts[3]: ID: 1434 Name: regular seat
'
' Remove("1534")
'
' ID: 1234 Name: crank arm
' ID: 1334 Name: chain ring
' ID: 1834 Name: brake lever
' ID: 1434 Name: regular seat
' ID: 1444 Name: banana seat
' ID: 1634 Name: shift lever
' '
' RemoveAt(3)
'
' ID: 1234 Name: crank arm
' ID: 1334 Name: chain ring
' ID: 1834 Name: brake lever
' ID: 1444 Name: banana seat
' ID: 1634 Name: shift lever
'
End Class
// 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
以下示例演示了类型字符串的泛型类的 List<T> 多个属性和方法。 有关复杂类型的 List<T> 示例,请参阅 Contains 方法。
无参数构造函数用于创建具有默认容量的字符串列表。 此时会显示该 Capacity 属性,然后使用 Add 该方法添加多个项。 列出项目后,再次显示Capacity属性和Count属性,以展示容量已根据需要增加。
该方法 Contains 用于测试列表中是否存在项, Insert 该方法用于在列表中间插入新项,并再次显示列表的内容。
默认 Item[Int32] 属性(C# 中的索引器)用于检索项, Remove 该方法用于删除前面添加的重复项的第一个实例,并再次显示内容。 该方法 Remove 始终删除它遇到的第一个实例。
TrimExcess 方法用于将容量减少至与计数匹配,并显示 Capacity 和 Count 属性。 如果未使用的容量小于总容量的 10%,则不会调整列表的大小。
最后,使用Clear方法删除列表中的所有项,然后显示Capacity和Count属性。
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
*/
Public Class Example2
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> 类是类的 ArrayList 泛型等效项。 它通过使用根据需要动态增加大小的数组来实现 IList<T> 泛型接口。
可以使用List<T>或Add方法将项目添加到AddRange中。
该 List<T> 类同时使用相等比较器和排序比较器。
方法(例如 Contains、IndexOf、LastIndexOf 和 Remove)使用相等比较器针对列表元素进行比较。 类型
T的默认相等比较器按如下方式确定。 如果类型T实现 IEquatable<T> 泛型接口,则相等比较器是 Equals(T) 该接口的方法;否则,默认相等比较器为 Object.Equals(Object)。BinarySearch和Sort等方法使用用于列表元素的排序比较器。 类型
T的默认比较器按如下方式确定。 如果类型T实现 IComparable<T> 泛型接口,则默认比较器是 CompareTo(T) 该接口的方法;否则,如果类型T实现非泛型 IComparable 接口,则默认比较器是 CompareTo(Object) 该接口的方法。 如果类型T未实现任何接口,则没有默认比较器,必须显式提供一个比较器或比较委托。
不能保证对 List<T> 进行排序。 必须在执行需要List<T>排序的操作(例如BinarySearch)之前先对List<T>进行排序。
可以使用整数索引访问此集合中的元素。 此集合中的索引从零开始。
List<T> 接受 null 为引用类型的有效值,并允许重复元素。
如需获取 List<T> 类的不可变版本,请参阅 ImmutableList<T>。
性能注意事项
在决定是否使用 List<T> 或 ArrayList 类时,这两者都具有类似的功能,请记住 List<T> ,在大多数情况下,该类的性能更好,并且类型安全。 如果将引用类型用于 T 类的类型 List<T>,那么这两个类的行为是相同的。 但是,如果值类型用于类型 T,则需要考虑实现和装箱问题。
如果将值类型用作类型 T,编译器将专门为该值类型生成一个 List<T> 类的实现。 这意味着,一个List<T>对象的列表元素在使用元素之前不必装箱,并且在创建大约 500 个列表元素后,通过不装箱列表元素节省的内存大于用于生成类实现所需的内存。
确保用于类型 T 的值类型实现 IEquatable<T> 泛型接口。 否则,像 Contains 这样的方法必须调用 Object.Equals(Object) 方法,以将受影响的列表元素装箱。 如果值类型实现了 IComparable 接口,并且您拥有源代码,还需实现 IComparable<T> 泛型接口,以避免 BinarySearch 和 Sort 方法对列表元素进行装箱。 如果你不拥有源代码,请将对象 IComparer<T> 传递给 BinarySearch 和 Sort 方法。
使用 List<T> 类的特定于类型的实现,而不是使用 ArrayList 类或自行编写强类型包装器集合,这对你更有利。 这是因为你的实现必须为你执行 .NET 的作用,而 .NET 运行时可以共享你的实现无法实现的公共中间语言代码和元数据。
F# 注意事项
该 List<T> 类在 F# 代码中不常使用。 相反,列表,这些是不可变的单链列表,通常更受青睐。 F# List 提供有序的不可变值系列,并且支持在功能样式开发中使用。 从 F# 使用时,List<T> 类通常通过 ResizeArray<'T> 类型缩写引用,以避免与 F# 列表的命名冲突。
构造函数
| 名称 | 说明 |
|---|---|
| List<T>() |
初始化为空且具有默认初始容量的 List<T> 类的新实例。 |
| List<T>(IEnumerable<T>) |
初始化类的新实例,该实例 List<T> 包含从指定集合复制的元素,并且有足够的容量来容纳复制的元素数。 |
| List<T>(Int32) |
初始化类的新实例,该实例 List<T> 为空且具有指定的初始容量。 |
属性
| 名称 | 说明 |
|---|---|
| Capacity |
获取或设置内部数据结构可以在不调整大小的情况下保留的元素总数。 |
| Count |
获取包含在 . 中的 List<T>元素数。 |
| Item[Int32] |
获取或设置指定索引处的元素。 |
方法
| 名称 | 说明 |
|---|---|
| Add(T) |
将对象添加到该 List<T>对象的末尾。 |
| AddRange(IEnumerable<T>) |
将指定集合的元素添加到该 List<T>集合的末尾。 |
| AsReadOnly() |
返回当前集合的只读 ReadOnlyCollection<T> 包装器。 |
| BinarySearch(Int32, Int32, T, IComparer<T>) |
使用指定的比较器搜索已排序 List<T> 的元素范围,并返回该元素的从零开始的索引。 |
| BinarySearch(T, IComparer<T>) |
使用指定的比较器搜索整个排序 List<T> 的元素,并返回元素的从零开始的索引。 |
| BinarySearch(T) |
使用默认比较器搜索整个排序 List<T> 的元素,并返回元素的从零开始的索引。 |
| Clear() |
从 .. 中删除所有元素List<T> |
| Contains(T) |
确定元素是否在 List<T>. |
| ConvertAll<TOutput>(Converter<T,TOutput>) |
将当前 List<T> 中的元素转换为另一种类型,并返回包含已转换元素的列表。 |
| CopyTo(Int32, T[], Int32, Int32) |
从 List<T> 目标数组的指定索引处开始,从兼容的一维数组复制一系列元素。 |
| CopyTo(T[], Int32) |
将整个 List<T> 复制到兼容的一维数组,从目标数组的指定索引处开始。 |
| CopyTo(T[]) |
将整个复制到 List<T> 兼容的一维数组,从目标数组的开头开始。 |
| EnsureCapacity(Int32) |
确保此列表的容量至少是指定的 |
| Equals(Object) |
确定指定的对象是否等于当前对象。 (继承自 Object) |
| Exists(Predicate<T>) |
确定 List<T> 包含与指定谓词定义的条件匹配的元素。 |
| Find(Predicate<T>) |
搜索与指定谓词定义的条件匹配的元素,并返回整个 List<T>中的第一个匹配项。 |
| FindAll(Predicate<T>) |
检索与指定谓词定义的条件匹配的所有元素。 |
| FindIndex(Int32, Int32, Predicate<T>) |
搜索与指定谓词定义的条件匹配的元素,并返回从指定索引开始且包含指定数量的元素 List<T> 范围内第一个匹配项的从零开始的索引。 |
| FindIndex(Int32, Predicate<T>) |
搜索与指定谓词定义的条件匹配的元素,并在从指定索引扩展到最后一个元素的元素 List<T> 范围内返回第一个匹配项的从零开始的索引。 |
| FindIndex(Predicate<T>) |
搜索与指定谓词定义的条件匹配的元素,并返回整个 List<T>中第一个匹配项的从零开始的索引。 |
| FindLast(Predicate<T>) |
搜索与指定谓词定义的条件匹配的元素,并返回整个 List<T>中的最后一个匹配项。 |
| FindLastIndex(Int32, Int32, Predicate<T>) |
搜索与指定谓词定义的条件匹配的元素,并在包含指定索引的元素 List<T> 范围内返回最后一个匹配项的从零开始的索引。 |
| FindLastIndex(Int32, Predicate<T>) |
搜索与指定谓词定义的条件匹配的元素,并在从第一个元素扩展到指定索引的元素 List<T> 范围内返回最后一个匹配项的从零开始的索引。 |
| FindLastIndex(Predicate<T>) |
搜索与指定谓词定义的条件匹配的元素,并返回整个 List<T>中最后一个匹配项的从零开始的索引。 |
| ForEach(Action<T>) |
对每个元素 List<T>执行指定的操作。 |
| GetEnumerator() |
返回循环访问的 List<T>枚举数。 |
| GetHashCode() |
用作默认哈希函数。 (继承自 Object) |
| GetRange(Int32, Int32) |
在源 List<T>中创建一系列元素的浅表副本。 |
| GetType() |
获取当前实例的 Type。 (继承自 Object) |
| IndexOf(T, Int32, Int32) |
搜索指定的对象,并返回从指定索引开始的元素 List<T> 范围内第一个匹配项的从零开始的索引,并包含指定的元素数。 |
| IndexOf(T, Int32) |
搜索指定的对象,并在从指定索引扩展到最后一个元素的元素 List<T> 范围内返回第一个匹配项的从零开始的索引。 |
| IndexOf(T) |
搜索指定的对象并返回整个 List<T>中第一个匹配项的从零开始的索引。 |
| Insert(Int32, T) |
将元素 List<T> 插入指定索引处。 |
| InsertRange(Int32, IEnumerable<T>) |
将集合的元素插入 List<T> 指定索引处。 |
| LastIndexOf(T, Int32, Int32) |
搜索指定的对象,并返回包含指定数量的元素 List<T> 并在指定索引处结束的最后一个匹配项范围内的最后一个匹配项的从零开始的索引。 |
| LastIndexOf(T, Int32) |
搜索指定的对象,并返回从第一个元素扩展到指定索引的元素 List<T> 范围内最后一个匹配项的从零开始的索引。 |
| LastIndexOf(T) |
搜索指定的对象,并返回整个 List<T>内最后一个匹配项的从零开始的索引。 |
| MemberwiseClone() |
创建当前 Object的浅表副本。 (继承自 Object) |
| Remove(T) |
从 List<T>中删除特定对象的第一个匹配项。 |
| RemoveAll(Predicate<T>) |
删除与指定谓词定义的条件匹配的所有元素。 |
| RemoveAt(Int32) |
移除位于指定索引处的 List<T>元素。 |
| RemoveRange(Int32, Int32) |
从 . 中删除一系列元素 List<T>。 |
| Reverse() |
反转整个 List<T>元素的顺序。 |
| Reverse(Int32, Int32) |
反转指定区域中元素的顺序。 |
| Slice(Int32, Int32) |
在源 List<T>中创建一系列元素的浅表副本。 |
| Sort() |
使用默认比较器对整个 List<T> 元素进行排序。 |
| Sort(Comparison<T>) |
使用指定的List<T>元素对整个Comparison<T>元素进行排序。 |
| Sort(IComparer<T>) |
使用指定的比较器对整个 List<T> 元素进行排序。 |
| Sort(Int32, Int32, IComparer<T>) |
使用指定的比较器对一系列元素中的 List<T> 元素进行排序。 |
| ToArray() |
将元素 List<T> 复制到新数组。 |
| ToString() |
返回一个表示当前对象的字符串。 (继承自 Object) |
| TrimExcess() |
如果该数字小于阈值,则将容量设置为实际 List<T>元素数。 |
| TrueForAll(Predicate<T>) |
确定每个 List<T> 元素是否与指定谓词定义的条件匹配。 |
显式接口实现
| 名称 | 说明 |
|---|---|
| ICollection.CopyTo(Array, Int32) |
将元素ICollection复制到从特定Array索引开始的元素Array。 |
| ICollection.IsSynchronized |
获取一个值,该值指示对 ICollection 同步的访问是否同步(线程安全)。 |
| ICollection.SyncRoot |
获取可用于同步对 . ICollection的访问的对象。 |
| ICollection<T>.IsReadOnly |
获取一个值,该值指示是否 ICollection<T> 为只读。 |
| IEnumerable.GetEnumerator() |
返回循环访问集合的枚举器。 |
| IEnumerable<T>.GetEnumerator() |
返回循环访问集合的枚举器。 |
| IList.Add(Object) |
将项添加到 IList。 |
| IList.Contains(Object) |
确定 IList 是否包含特定值。 |
| IList.IndexOf(Object) |
确定 IList中特定项的索引。 |
| IList.Insert(Int32, Object) |
将项插入 IList 到指定索引处。 |
| IList.IsFixedSize |
获取一个值,该值指示是否 IList 具有固定大小。 |
| IList.IsReadOnly |
获取一个值,该值指示是否 IList 为只读。 |
| IList.Item[Int32] |
获取或设置指定索引处的元素。 |
| IList.Remove(Object) |
从 IList中删除特定对象的第一个匹配项。 |
扩展方法
适用于
线程安全性
此类型的公共静态(Shared 在 Visual Basic 中)成员是线程安全的。 不能保证任何实例成员是线程安全的。
可以安全地对某个 List<T>集合执行多个读取操作,但如果在读取集合时修改了集合,则可能会出现问题。 若要确保线程安全,请在读取或写入操作期间锁定集合。 若要允许多个线程访问集合进行读取和写入,必须实现自己的同步。 有关具有内置同步的集合,请参阅命名空间中的 System.Collections.Concurrent 类。 有关固有线程安全的替代方法,请参阅该 ImmutableList<T> 类。