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
次の例では、string 型の 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の既定の等値比較子は、次のように決定されます。 型TIEquatable<T>ジェネリック インターフェイスを実装する場合、等値比較子はそのインターフェイスのEquals(T) メソッドです。それ以外の場合、既定の等値比較子はObject.Equals(Object)。BinarySearchやSortなどのメソッドは、リスト要素に順序比較子を使用します。 型
Tの既定の比較子は、次のように決定されます。 型TIComparable<T>ジェネリック インターフェイスを実装する場合、既定の比較子はそのインターフェイスのCompareTo(T) メソッドです。それ以外の場合、型Tが非ジェネリック IComparable インターフェイスを実装する場合、既定の比較子はそのインターフェイスのCompareTo(Object)メソッドになります。 型Tがどちらのインターフェイスも実装していない場合、既定の比較子はなく、比較子または比較デリゲートを明示的に指定する必要があります。
List<T> が並べ替えられる保証はありません。 List<T>を並べ替える必要がある操作 (BinarySearch など) を実行する前に、List<T>を並べ替える必要があります。
このコレクション内の要素には、整数インデックスを使用してアクセスできます。 このコレクション内のインデックスは 0 から始まります。
List<T> は、参照型の有効な値として null を受け取り、重複する要素を許可します。
List<T> クラスの不変バージョンについては、ImmutableList<T>を参照してください。
パフォーマンスに関する考慮事項
どちらも同様の機能を持つ List<T> クラスと ArrayList クラスのどちらを使用するかを決定する際には、ほとんどの場合、 List<T> クラスの方がパフォーマンスが高く、タイプ セーフであることを覚えておいてください。
T クラスの型List<T>に参照型を使用する場合、2 つのクラスの動作は同じです。 ただし、値型を型 Tに使用する場合は、実装とボックス化の問題を考慮する必要があります。
型 Tに値型が使用されている場合、コンパイラは、その値型専用の List<T> クラスの実装を生成します。 つまり、 List<T> オブジェクトのリスト要素は、要素を使用する前にボックス化する必要はありません。また、約 500 個のリスト要素が作成された後、リスト要素をボックス化しないことによって保存されるメモリは、クラス実装の生成に使用されるメモリよりも大きくなります。
T型として使用する値型がIEquatable<T>ジェネリックインターフェースを実装していることを確認します。 そうでない場合、 Contains などのメソッドは、影響を受ける list 要素をボックス化する Object.Equals(Object) メソッドを呼び出す必要があります。 値型が IComparable インターフェイスを実装し、ソース コードを所有している場合は、 IComparable<T> ジェネリック インターフェイスも実装して、 BinarySearch メソッドと Sort メソッドがリスト要素をボックス化しないようにします。 ソース コードを所有していない場合は、 IComparer<T> オブジェクトを BinarySearch メソッドと Sort メソッドに渡します。
List<T> クラスを使用したり、厳密に型指定されたラッパー コレクションを自分で記述したりする代わりに、ArrayList クラスの型固有の実装を使用することが利点です。 これは、実装で .NET が既に行っていることを行う必要があり、.NET ランタイムが共通の中間言語コードとメタデータを共有できるためです。これは、実装では共有できません。
F# 考慮事項
List<T> クラスは、F# コードでは頻繁に使用しません。 代 わりに、変更できない、1 つのリンクされたリストであるリストが一般的に推奨されます。 F# List は順序付けされた不変の一連の値を提供し、機能スタイルの開発で使用するためにサポートされています。 F# から使用する場合、 List<T> クラスは通常、F# リストとの名前付けの競合を回避するために、 ResizeArray<'T> 型の省略形によって参照されます。
コンストラクター
| 名前 | 説明 |
|---|---|
| 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> 内の要素の範囲を検索し、要素の 0 から始まるインデックスを返します。 |
| BinarySearch(T, IComparer<T>) |
指定した比較子を使用して、並べ替えられた List<T> 全体を検索し、要素の 0 から始まるインデックスを返します。 |
| BinarySearch(T) |
既定の比較子を使用して、並べ替えられた List<T> 全体を検索し、要素の 0 から始まるインデックスを返します。 |
| Clear() |
List<T>からすべての要素を削除します。 |
| Contains(T) |
要素が List<T>内にあるかどうかを判断します。 |
| ConvertAll<TOutput>(Converter<T,TOutput>) |
現在の List<T> 内の要素を別の型に変換し、変換された要素を含むリストを返します。 |
| CopyTo(Int32, T[], Int32, Int32) |
ターゲット配列の指定したインデックスから始まる、 List<T> から互換性のある 1 次元配列に要素の範囲をコピーします。 |
| CopyTo(T[], Int32) |
ターゲット配列の指定したインデックスから始まる互換性のある 1 次元配列に、 List<T> 全体をコピーします。 |
| CopyTo(T[]) |
ターゲット配列の先頭から、互換性のある 1 次元配列に 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> 内の要素の範囲内で最初に出現する 0 から始まるインデックスを返します。 |
| FindIndex(Int32, Predicate<T>) |
指定した述語で定義された条件に一致する要素を検索し、指定したインデックスから最後の要素まで拡張される List<T> 内の要素の範囲内で最初に出現する 0 から始まるインデックスを返します。 |
| FindIndex(Predicate<T>) |
指定した述語で定義されている条件に一致する要素を検索し、 List<T>全体で最初に見つかった位置の 0 から始まるインデックスを返します。 |
| FindLast(Predicate<T>) |
指定した述語で定義されている条件に一致する要素を検索し、 List<T>全体の最後の出現箇所を返します。 |
| FindLastIndex(Int32, Int32, Predicate<T>) |
指定した述語で定義された条件に一致する要素を検索し、指定した数の要素を含み、指定したインデックスで終わる、 List<T> 内の要素の範囲内で最後に出現する 0 から始まるインデックスを返します。 |
| FindLastIndex(Int32, Predicate<T>) |
指定した述語で定義された条件に一致する要素を検索し、最初の要素から指定したインデックスまで拡張される List<T> 内の要素の範囲内で最後に出現する 0 から始まるインデックスを返します。 |
| FindLastIndex(Predicate<T>) |
指定した述語で定義されている条件に一致する要素を検索し、 List<T>全体で最後に見つかった位置の 0 から始まるインデックスを返します。 |
| ForEach(Action<T>) |
List<T>の各要素に対して指定されたアクションを実行します。 |
| GetEnumerator() |
List<T>を反復処理する列挙子を返します。 |
| GetHashCode() |
既定のハッシュ関数として機能します。 (継承元 Object) |
| GetRange(Int32, Int32) |
ソース List<T>内の要素の範囲の浅いコピーを作成します。 |
| GetType() |
現在のインスタンスの Type を取得します。 (継承元 Object) |
| IndexOf(T, Int32, Int32) |
指定したオブジェクトを検索し、指定したインデックスから始まり、指定した数の要素を含む List<T> 内の要素の範囲内で最初に出現する 0 から始まるインデックスを返します。 |
| IndexOf(T, Int32) |
指定したオブジェクトを検索し、指定したインデックスから最後の要素までの List<T> 内の要素の範囲内で最初に見つかった位置の 0 から始まるインデックスを返します。 |
| IndexOf(T) |
指定したオブジェクトを検索し、 List<T>全体で最初に見つかった位置の 0 から始まるインデックスを返します。 |
| Insert(Int32, T) |
指定したインデックス位置にある List<T> に要素を挿入します。 |
| InsertRange(Int32, IEnumerable<T>) |
コレクションの要素を、指定したインデックス位置にある List<T> に挿入します。 |
| LastIndexOf(T, Int32, Int32) |
指定したオブジェクトを検索し、指定した数の要素を含み、指定したインデックスで終わる、 List<T> 内の要素の範囲内で最後に出現した位置の 0 から始まるインデックスを返します。 |
| LastIndexOf(T, Int32) |
指定したオブジェクトを検索し、最初の要素から指定したインデックスに拡張される List<T> 内の要素の範囲内で最後に出現する 0 から始まるインデックスを返します。 |
| LastIndexOf(T) |
指定したオブジェクトを検索し、 List<T>全体で最後に出現した位置の 0 から始まるインデックスを返します。 |
| 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から削除します。 |
拡張メソッド
適用対象
スレッド セーフ
この型のパブリック静的 (Visual Basic のShared ) メンバーはスレッド セーフです。 インスタンス メンバーがスレッド セーフであるとは限りません。
List<T>に対して複数の読み取り操作を実行しても安全ですが、コレクションが読み取り中に変更されると問題が発生する可能性があります。 スレッド セーフを確保するには、読み取りまたは書き込み操作中にコレクションをロックします。 読み取りと書き込みのためにコレクションに複数のスレッドからアクセスできるようにするには、独自の同期を実装する必要があります。 同期が組み込まれているコレクションについては、 System.Collections.Concurrent 名前空間のクラスを参照してください。 本質的にスレッド セーフな代替手段については、 ImmutableList<T> クラスを参照してください。