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
[<System.Serializable>]
type List<'T> = class
interface IList<'T>
interface IList
interface IReadOnlyList<'T>
interface ICollection<'T>
interface seq<'T>
interface IEnumerable
interface ICollection
interface IReadOnlyCollection<'T>
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
*/
}
}
Imports 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
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[] のプロパティ (C# のインデクサー) は項目を取得するために使用され、 Remove メソッドは前に追加した重複する項目の最初のインスタンスを削除するために使用され、内容が再び表示されます。 このメソッドは Remove 、最初に見つかったインスタンスを常に削除します。
この TrimExcess メソッドは、カウントに合わせて容量を減らすために使用され Capacity 、プロパティとプロパティ Count が表示されます。 未使用の容量が合計容量の 10% 未満の場合、リストのサイズは変更されませんでした。
最後に、このメソッドをClear使用してリストからすべての項目を削除し、プロパティとCountプロパティを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> 、クラスと同等の ArrayList ジェネリックです。 必要に応じてサイズが IList<T> 動的に増加する配列を使用して、ジェネリック インターフェイスを実装します。
またはAddRangeメソッドを使用して、項目を a List<T> にAdd追加できます。
クラスは List<T> 、等値比較子と順序比較子の両方を使用します。
などのContainsIndexOfLastIndexOfメソッドで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> 、並べ替える必要があります。
このコレクション内の要素には、整数インデックスを使用してアクセスできます。 このコレクション内のインデックスは 0 から始まります。
.NET Frameworkのみ: 非常に大きなList<T>オブジェクトの場合、構成要素の属性 <gcAllowVeryLargeObjects>
を実行時環境に設定enabled
することで、64 ビット システムで最大容量を 20 億要素にtrue
増やすことができます。
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 メソッドは、影響を Object.Equals(Object) 受ける list 要素をボックス化するメソッドを呼び出す必要があります。 値の型がインターフェイスをIComparable実装し、ソース コードを所有している場合は、ジェネリック インターフェイスも実装IComparable<T>して、リスト要素をSortボックス化しないようにBinarySearchします。 ソース コードを所有していない場合は、オブジェクトを IComparer<T> and Sort メソッドにBinarySearch渡します。
クラスを使用したり、厳密に型指定されたラッパー コレクションを自分で記述したりする代わりに、クラスの型固有の実装 List<T> を使用 ArrayList することが利点です。 これは、実装で.NET Frameworkが既に行っていることを行う必要があり、共通言語ランタイムが Microsoft の中間言語コードとメタデータを共有できるためです。これは実装ではできません。
F# に関する考慮事項
この List<T> クラスは、F# コードではあまり使用しません。 代 わりに、変更できないリスト (1 つ 1 つリンクされたリスト) が一般的に推奨されます。 F# List
は順序付けされた不変の一連の値を提供し、機能スタイルの開発で使用するためにサポートされています。 F# から使用する場合、F# リストとの名前の競合を回避するために、 List<T> クラスは通常、型の省略形によって 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> の 1 つの要素の範囲を検索し、その要素の 0 から始まるインデックスを返します。 |
BinarySearch(T) |
既定の比較子を使用して、並べ替えられた要素の List<T> 全体を検索し、その要素の 0 から始まるインデックスを返します。 |
BinarySearch(T, IComparer<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[]) |
List<T> 全体を互換性のある 1 次元の配列にコピーします。コピー操作は、コピー先の配列の先頭から始まります。 |
CopyTo(T[], Int32) |
List<T> 全体を、互換性のある 1 次元配列の、指定したインデックスから始まる位置にコピーします。 |
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) |
List<T> 全体から指定したオブジェクトを検索し、最初に見つかったオブジェクトのインデックス (0 から始まる) を返します。 |
IndexOf(T, Int32) |
List<T> のうち指定したインデックスから最後の要素までの要素範囲の中から、指定したオブジェクトを検索し、最初に出現する位置の 0 から始まるインデックス番号を返します。 |
IndexOf(T, Int32, Int32) |
指定したインデックスから始まり、指定した数の要素が含まれる List<T> の要素範囲内で、指定したオブジェクトを検索し、最初に出現する位置の 0 から始まるインデックス番号を返します。 |
Insert(Int32, T) |
List<T> 内の指定したインデックスの位置に要素を挿入します。 |
InsertRange(Int32, IEnumerable<T>) |
コレクションの要素を List<T> 内の指定したインデックスの位置に挿入します。 |
LastIndexOf(T) |
List<T> 全体から指定したオブジェクトを検索し、最後に見つかったオブジェクトのインデックス (0 から始まる) を返します。 |
LastIndexOf(T, Int32) |
List<T> のうち、最初の要素から指定したインデックスまでの要素範囲の中で、指定したオブジェクトを検索し、最後に出現する位置の 0 から始まるインデックス番号を返します。 |
LastIndexOf(T, Int32, Int32) |
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) |
指定した範囲の要素の順序を反転させます。 |
Sort() |
既定の比較子を使用して、List<T> 全体内の要素を並べ替えます。 |
Sort(Comparison<T>) |
指定した Comparison<T> を使用して、List<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
) なこの型のメンバーはスレッド セーフです インスタンス メンバーの場合は、スレッド セーフであるとは限りません。
1 つのコレクションに対して複数の読み取り操作を List<T>実行しても安全ですが、コレクションが読み取り中に変更されると問題が発生する可能性があります。 スレッド セーフを確保するには、読み取りまたは書き込み操作中にコレクションをロックします。 コレクションに読み取りと書き込みのために複数のスレッドからアクセスできるようにするには、独自の同期を実装する必要があります。 組み込みの同期を使用するコレクションについては、名前空間内のクラスを System.Collections.Concurrent 参照してください。 本質的にスレッド セーフな代替手段については、クラスを ImmutableList<T> 参照してください。