コレクション【collection】
Collection クラス
アセンブリ: Microsoft.VisualBasic (microsoft.visualbasic.dll 内)

<SerializableAttribute> _ Public NotInheritable Class Collection Implements ICollection, IList, ISerializable, IDeserializationCallback
[SerializableAttribute] public sealed class Collection : ICollection, IList, ISerializable, IDeserializationCallback
[SerializableAttribute] public ref class Collection sealed : ICollection, IList, ISerializable, IDeserializationCallback
/** @attribute SerializableAttribute() */ public final class Collection implements ICollection, IList, ISerializable, IDeserializationCallback
SerializableAttribute public final class Collection implements ICollection, IList, ISerializable, IDeserializationCallback

詳細については、Visual Basic のトピック「Collection オブジェクト (Visual Basic)」を参照してください。
Visual Basic の Collection オブジェクトは、関連する項目のグループを 1 つのオブジェクトとして参照する場合に便利です。コレクション内の項目、つまり要素に必要な関連性は、コレクション内に存在しているという事実だけです。コレクションの要素は同じデータ型である必要はありません。
次の例に示すように、他のオブジェクトの作成と同じ方法でコレクションを作成できます。
-
For Each...Next ステートメント (Visual Basic) で、コレクション全体を反復処理する。
注意
Visual Basic の Collection の反復処理は、スレッド セーフな処理ではありません。コレクションの同期がとられている場合でも、別のスレッドによってそのコレクションを変更できるため、変更の結果として列挙子は例外をスローします。列挙の処理をスレッド セーフにするには、コレクションをロックする方法と、別のスレッドによる変更で発生した例外をキャッチする方法があります。プログラミング要素のロックの詳細については、「SyncLock ステートメント」を参照してください。

次の例では、Collection オブジェクト (names) と、ユーザーがオブジェクト (名前) をコレクションに追加するためのダイアログ ボックスを作成しています。さらにコレクション内の名前を表示し、最後に Collection オブジェクト自体を破棄しないでコレクションを空にします。
この例がどのように動作するかを確認するには、[プロジェクト] メニューの [クラスの追加] コマンドをクリックし、各インスタンスの名前を保持する変数として instanceName という名前のパブリック変数を nameClass のモジュール レベルで宣言します (Public instanceName と入力)。既定の名前は nameClass のままです。次のコードをコピーして別のモジュールの全般セクションに貼り付けて、別のプロシージャでそのモジュールを classNamer ステートメントで開始します。この例は、クラスをサポートするホスト アプリケーションでのみ動作します。
Public Class nameClass Public instanceName As String End Class Sub classNamer() ' Create a Visual Basic Collection object. Dim names As New Microsoft.VisualBasic.Collection() Dim key As Integer Dim msg As String Dim name As String Dim nameList As String = "" ' 1. Get names from the user to add to the collection. Do Dim inst As New nameClass() key += 1 msg = "Please enter a name for this object." & vbCrLf _ & "Press Cancel to see names in collection." name = InputBox(msg, "Name the Collection items") inst.instanceName = name ' If user entered a name, add it to the collection. If inst.instanceName <> "" Then names.Add(inst, CStr(key)) End If Loop Until name = "" ' 2. Create and display a list of names from the collection. For Each oneInst As nameClass In names nameList &= oneInst.instanceName & vbCrLf Next oneInst MsgBox(nameList, , "Instance Names in names Collection") ' 3. Remove elements from the collection without disposing of the collection. For count As Integer = 1 To names.Count names.Remove(1) ' Since Visual Basic collections are reindexed automatically, ' remove the first member on each iteration. Next count End Sub

Microsoft.VisualBasic.Collection


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Collection メンバ
Microsoft.VisualBasic 名前空間
その他の技術情報
Collection オブジェクト (Visual Basic)
Collection オブジェクトのメンバ
GetEnumerator メソッド (コレクション オブジェクト)
Collection コンストラクタ ()
アセンブリ: mscorlib (mscorlib.dll 内)



Collection が備える多数のプロパティとメソッドを使用した例を次に示します。このコード例では、Collection コンストラクタで文字列のコレクションを作成し、Add メソッドを使用していくつかの文字列を追加し、Count を表示して、文字列を一覧表示します。IndexOf メソッドを使用して文字列のインデックスを検索し、Contains メソッドを使用して文字列がコレクションにあるかどうかを判断します。Insert メソッドを使用して文字列を挿入し、既定の Item プロパティ (C# の場合はインデクサ) を使用して文字列を取得および設定します。文字列 ID による文字列の削除のために Remove メソッドを使用し、インデックスによる文字列の削除のために RemoveAt メソッドを使用します。最後に、Clear メソッドを使用してコレクションのすべての文字列をクリアします。
Imports System Imports System.Collections.Generic Imports System.Collections.ObjectModel Public Class Demo Public Shared Sub Main() Dim dinosaurs As New Collection(Of String) dinosaurs.Add("Psitticosaurus") dinosaurs.Add("Caudipteryx") dinosaurs.Add("Compsognathus") dinosaurs.Add("Muttaburrasaurus") Console.WriteLine("{0} dinosaurs:", dinosaurs.Count) Display(dinosaurs) Console.WriteLine(vbLf & "IndexOf(""Muttaburrasaurus""): {0}", _ dinosaurs.IndexOf("Muttaburrasaurus")) Console.WriteLine(vbLf & "Contains(""Caudipteryx""): {0}", _ dinosaurs.Contains("Caudipteryx")) Console.WriteLine(vbLf & "Insert(2, ""Nanotyrannus"")") dinosaurs.Insert(2, "Nanotyrannus") Display(dinosaurs) Console.WriteLine(vbLf & "dinosaurs(2): {0}", dinosaurs(2)) Console.WriteLine(vbLf & "dinosaurs(2) = ""Microraptor""") dinosaurs(2) = "Microraptor" Display(dinosaurs) Console.WriteLine(vbLf & "Remove(""Microraptor"")") dinosaurs.Remove("Microraptor") Display(dinosaurs) Console.WriteLine(vbLf & "RemoveAt(0)") dinosaurs.RemoveAt(0) Display(dinosaurs) Console.WriteLine(vbLf & "dinosaurs.Clear()") dinosaurs.Clear() Console.WriteLine("Count: {0}", dinosaurs.Count) End Sub Private Shared Sub Display(ByVal cs As Collection(Of String)) Console.WriteLine() For Each item As String In cs Console.WriteLine(item) Next item End Sub End Class ' This code example produces the following output: ' '4 dinosaurs: ' 'Psitticosaurus 'Caudipteryx 'Compsognathus 'Muttaburrasaurus ' 'IndexOf("Muttaburrasaurus"): 3 ' 'Contains("Caudipteryx"): True ' 'Insert(2, "Nanotyrannus") ' 'Psitticosaurus 'Caudipteryx 'Nanotyrannus 'Compsognathus 'Muttaburrasaurus ' 'dinosaurs(2): Nanotyrannus ' 'dinosaurs(2) = "Microraptor" ' 'Psitticosaurus 'Caudipteryx 'Microraptor 'Compsognathus 'Muttaburrasaurus ' 'Remove("Microraptor") ' 'Psitticosaurus 'Caudipteryx 'Compsognathus 'Muttaburrasaurus ' 'RemoveAt(0) ' 'Caudipteryx 'Compsognathus 'Muttaburrasaurus ' 'dinosaurs.Clear() 'Count: 0
using System; using System.Collections.Generic; using System.Collections.ObjectModel; public class Demo { public static void Main() { Collection<string> dinosaurs = new Collection<string>(); dinosaurs.Add("Psitticosaurus"); dinosaurs.Add("Caudipteryx"); dinosaurs.Add("Compsognathus"); dinosaurs.Add("Muttaburrasaurus"); Console.WriteLine("{0} dinosaurs:", dinosaurs.Count); Display(dinosaurs); Console.WriteLine("\nIndexOf(\"Muttaburrasaurus\"): {0}", dinosaurs.IndexOf("Muttaburrasaurus")); Console.WriteLine("\nContains(\"Caudipteryx\"): {0}", dinosaurs.Contains("Caudipteryx")); Console.WriteLine("\nInsert(2, \"Nanotyrannus\")"); dinosaurs.Insert(2, "Nanotyrannus"); Display(dinosaurs); Console.WriteLine("\ndinosaurs[2]: {0}", dinosaurs[2]); Console.WriteLine("\ndinosaurs[2] = \"Microraptor\""); dinosaurs[2] = "Microraptor"; Display(dinosaurs); Console.WriteLine("\nRemove(\"Microraptor\")"); dinosaurs.Remove("Microraptor"); Display(dinosaurs); Console.WriteLine("\nRemoveAt(0)"); dinosaurs.RemoveAt(0); Display(dinosaurs); Console.WriteLine("\ndinosaurs.Clear()"); dinosaurs.Clear(); Console.WriteLine("Count: {0}", dinosaurs.Count); } private static void Display(Collection<string> cs) { Console.WriteLine(); foreach( string item in cs ) { Console.WriteLine(item); } } } /* This code example produces the following output: 4 dinosaurs: Psitticosaurus Caudipteryx Compsognathus Muttaburrasaurus IndexOf("Muttaburrasaurus"): 3 Contains("Caudipteryx"): True Insert(2, "Nanotyrannus") Psitticosaurus Caudipteryx Nanotyrannus Compsognathus Muttaburrasaurus dinosaurs[2]: Nanotyrannus dinosaurs[2] = "Microraptor" Psitticosaurus Caudipteryx Microraptor Compsognathus Muttaburrasaurus Remove("Microraptor") Psitticosaurus Caudipteryx Compsognathus Muttaburrasaurus RemoveAt(0) Caudipteryx Compsognathus Muttaburrasaurus dinosaurs.Clear() Count: 0 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Collection コンストラクタ
アセンブリ: Microsoft.VisualBasic (microsoft.visualbasic.dll 内)


このコンストラクタによって返された Visual Basic コレクションは空であり、初期量が割り当てられていません。
Visual Basic コレクションには、System.Collections、System.Collections.Generic、および System.Collections.Specialized の各名前空間で使用できる .NET Framework コレクションとの互換性はありません。

新しい Visual Basic コレクションを作成して変数 coll に割り当てる例を次に示します。
この Collection オブジェクトはインデックス番号が 1 から始まるオブジェクトであり、要素のインデックス値は 1 以上 Count プロパティ値以下の範囲です。Visual Basic コレクションは型 Object の要素を保持します。

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Collection コンストラクタ

名前 | 説明 |
---|---|
Collection () | Collection クラスの新しい空のインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
Collection (ジェネリック IList) | 指定したリストのラッパーとして Collection クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |

Collection コンストラクタ (ジェネリック IList)
アセンブリ: mscorlib (mscorlib.dll 内)




Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Collection ジェネリック クラス
アセンブリ: mscorlib (mscorlib.dll 内)

<SerializableAttribute> _ <ComVisibleAttribute(False)> _ Public Class Collection(Of T) Implements IList(Of T), ICollection(Of T), _ IEnumerable(Of T), IList, ICollection, _ IEnumerable
[SerializableAttribute] [ComVisibleAttribute(false)] public class Collection<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
[SerializableAttribute] [ComVisibleAttribute(false)] generic<typename T> public ref class Collection : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable

Collection クラスは、いずれかの構築型のインスタンスを作成することによってすぐに使用できます。コレクションに含めるオブジェクトの型を指定する以外の操作は必要ありません。また、任意の構築型から固有のコレクション型を派生させることや、Collection クラス自体からジェネリック コレクション型を派生させることもできます。
Collection クラスには、項目を追加および削除するときにコレクションをクリアしたり既存の項目の値を設定するための動作のカスタマイズに使用できるプロテクト メソッドがあります。
Collection インスタンスは常に変更できます。このクラスの読み取り専用バージョンについては、ReadOnlyCollection のトピックを参照してください。
このコレクション内の要素は、整数インデックスを使用してアクセスできます。このコレクションのインデックスは 0 から始まります。
Collection は、null 参照 (Visual Basic では Nothing) を参照型に対して有効な値として受け取り、要素の重複を許可します。
実装時の注意 この基本クラスは、カスタム コレクションを簡単に作成できるように提供されています。実装する場合は、独自のクラスを作成するのではなく、この基本クラスを拡張してください。
このセクションには、2 つのコード例が含まれています。最初の例では、Collection クラスのいくつかのプロパティおよびメソッドの例を示します。2 番目の例では、Collection の構築型からコレクション クラスを派生させる方法、および Collection のプロテクト メソッドをオーバーライドしてカスタム動作を提供する方法を示します。
Collection が備える多数のプロパティとメソッドを使用した例を次に示します。このコード例では、文字列のコレクションを作成し、Add メソッドを使用していくつかの文字列を追加し、Count を表示して、文字列を一覧表示します。IndexOf メソッドを使用して文字列のインデックスを検索し、Contains メソッドを使用して文字列がコレクションにあるかどうかを判断します。Insert メソッドを使用して文字列を挿入し、既定の Item プロパティ (C# の場合はインデクサ) を使用して文字列を取得および設定します。文字列 ID による文字列の削除のために Remove メソッドを使用し、インデックスによる文字列の削除のために RemoveAt メソッドを使用します。最後に、Clear メソッドを使用してコレクションのすべての文字列をクリアします。
Imports System Imports System.Collections.Generic Imports System.Collections.ObjectModel Public Class Demo Public Shared Sub Main() Dim dinosaurs As New Collection(Of String) dinosaurs.Add("Psitticosaurus") dinosaurs.Add("Caudipteryx") dinosaurs.Add("Compsognathus") dinosaurs.Add("Muttaburrasaurus") Console.WriteLine("{0} dinosaurs:", dinosaurs.Count) Display(dinosaurs) Console.WriteLine(vbLf & "IndexOf(""Muttaburrasaurus""): {0}", _ dinosaurs.IndexOf("Muttaburrasaurus")) Console.WriteLine(vbLf & "Contains(""Caudipteryx""): {0}", _ dinosaurs.Contains("Caudipteryx")) Console.WriteLine(vbLf & "Insert(2, ""Nanotyrannus"")") dinosaurs.Insert(2, "Nanotyrannus") Display(dinosaurs) Console.WriteLine(vbLf & "dinosaurs(2): {0}", dinosaurs(2)) Console.WriteLine(vbLf & "dinosaurs(2) = ""Microraptor""") dinosaurs(2) = "Microraptor" Display(dinosaurs) Console.WriteLine(vbLf & "Remove(""Microraptor"")") dinosaurs.Remove("Microraptor") Display(dinosaurs) Console.WriteLine(vbLf & "RemoveAt(0)") dinosaurs.RemoveAt(0) Display(dinosaurs) Console.WriteLine(vbLf & "dinosaurs.Clear()") dinosaurs.Clear() Console.WriteLine("Count: {0}", dinosaurs.Count) End Sub Private Shared Sub Display(ByVal cs As Collection(Of String)) Console.WriteLine() For Each item As String In cs Console.WriteLine(item) Next item End Sub End Class ' This code example produces the following output: ' '4 dinosaurs: ' 'Psitticosaurus 'Caudipteryx 'Compsognathus 'Muttaburrasaurus ' 'IndexOf("Muttaburrasaurus"): 3 ' 'Contains("Caudipteryx"): True ' 'Insert(2, "Nanotyrannus") ' 'Psitticosaurus 'Caudipteryx 'Nanotyrannus 'Compsognathus 'Muttaburrasaurus ' 'dinosaurs(2): Nanotyrannus ' 'dinosaurs(2) = "Microraptor" ' 'Psitticosaurus 'Caudipteryx 'Microraptor 'Compsognathus 'Muttaburrasaurus ' 'Remove("Microraptor") ' 'Psitticosaurus 'Caudipteryx 'Compsognathus 'Muttaburrasaurus ' 'RemoveAt(0) ' 'Caudipteryx 'Compsognathus 'Muttaburrasaurus ' 'dinosaurs.Clear() 'Count: 0
using System; using System.Collections.Generic; using System.Collections.ObjectModel; public class Demo { public static void Main() { Collection<string> dinosaurs = new Collection<string>(); dinosaurs.Add("Psitticosaurus"); dinosaurs.Add("Caudipteryx"); dinosaurs.Add("Compsognathus"); dinosaurs.Add("Muttaburrasaurus"); Console.WriteLine("{0} dinosaurs:", dinosaurs.Count); Display(dinosaurs); Console.WriteLine("\nIndexOf(\"Muttaburrasaurus\"): {0}", dinosaurs.IndexOf("Muttaburrasaurus")); Console.WriteLine("\nContains(\"Caudipteryx\"): {0}", dinosaurs.Contains("Caudipteryx")); Console.WriteLine("\nInsert(2, \"Nanotyrannus\")"); dinosaurs.Insert(2, "Nanotyrannus"); Display(dinosaurs); Console.WriteLine("\ndinosaurs[2]: {0}", dinosaurs[2]); Console.WriteLine("\ndinosaurs[2] = \"Microraptor\""); dinosaurs[2] = "Microraptor"; Display(dinosaurs); Console.WriteLine("\nRemove(\"Microraptor\")"); dinosaurs.Remove("Microraptor"); Display(dinosaurs); Console.WriteLine("\nRemoveAt(0)"); dinosaurs.RemoveAt(0); Display(dinosaurs); Console.WriteLine("\ndinosaurs.Clear()"); dinosaurs.Clear(); Console.WriteLine("Count: {0}", dinosaurs.Count); } private static void Display(Collection<string> cs) { Console.WriteLine(); foreach( string item in cs ) { Console.WriteLine(item); } } } /* This code example produces the following output: 4 dinosaurs: Psitticosaurus Caudipteryx Compsognathus Muttaburrasaurus IndexOf("Muttaburrasaurus"): 3 Contains("Caudipteryx"): True Insert(2, "Nanotyrannus") Psitticosaurus Caudipteryx Nanotyrannus Compsognathus Muttaburrasaurus dinosaurs[2]: Nanotyrannus dinosaurs[2] = "Microraptor" Psitticosaurus Caudipteryx Microraptor Compsognathus Muttaburrasaurus Remove("Microraptor") Psitticosaurus Caudipteryx Compsognathus Muttaburrasaurus RemoveAt(0) Caudipteryx Compsognathus Muttaburrasaurus dinosaurs.Clear() Count: 0 */
このコード例では、Collection ジェネリック クラスの構築型からコレクション クラスを派生させる方法、および InsertItem、RemoveItem、ClearItems、および SetItem の各プロテクト メソッドをオーバーライドして Add、Insert、Remove、および Clear の各メソッドのカスタム動作や、Item プロパティを設定するためのカスタム動作を提供する方法を示します。
この例で提供されるカスタム動作は、Changed という名前の通知イベントです。これは、各プロテクト メソッドの最後で発生します。Dinosaurs クラスは、Collection<string> (Visual Basic の場合は Collection(Of String)) を継承し、Changed イベントを定義します。イベント情報のために DinosaursChangedEventArgs クラスが使用され、変更の種類を識別するために列挙体が使用されます。
次のコード例では、Collection のいくつかのプロパティおよびメソッドを呼び出して、カスタム イベントの例を示します。
Imports System Imports System.Collections.Generic Imports System.Collections.ObjectModel Public Class Dinosaurs Inherits Collection(Of String) Public Event Changed As EventHandler(Of DinosaursChangedEventArgs) Protected Overrides Sub InsertItem( _ ByVal index As Integer, ByVal newItem As String) MyBase.InsertItem(index, newItem) RaiseEvent Changed(Me, New DinosaursChangedEventArgs( _ ChangeType.Added, newItem, Nothing)) End Sub Protected Overrides Sub SetItem(ByVal index As Integer, _ ByVal newItem As String) Dim replaced As String = Items(index) MyBase.SetItem(index, newItem) RaiseEvent Changed(Me, New DinosaursChangedEventArgs( _ ChangeType.Replaced, replaced, newItem)) End Sub Protected Overrides Sub RemoveItem(ByVal index As Integer) Dim removedItem As String = Items(index) MyBase.RemoveItem(index) RaiseEvent Changed(Me, New DinosaursChangedEventArgs( _ ChangeType.Removed, removedItem, Nothing)) End Sub Protected Overrides Sub ClearItems() MyBase.ClearItems() RaiseEvent Changed(Me, New DinosaursChangedEventArgs( _ ChangeType.Cleared, Nothing, Nothing)) End Sub End Class ' Event argument for the Changed event. ' Public Class DinosaursChangedEventArgs Inherits EventArgs Public ReadOnly ChangedItem As String Public ReadOnly ChangeType As ChangeType Public ReadOnly ReplacedWith As String Public Sub New(ByVal change As ChangeType, ByVal item As String, _ ByVal replacement As String) ChangeType = change ChangedItem = item ReplacedWith = replacement End Sub End Class Public Enum ChangeType Added Removed Replaced Cleared End Enum Public Class Demo Public Shared Sub Main() Dim dinosaurs As New Dinosaurs AddHandler dinosaurs.Changed, AddressOf ChangedHandler dinosaurs.Add("Psitticosaurus") dinosaurs.Add("Caudipteryx") dinosaurs.Add("Compsognathus") dinosaurs.Add("Muttaburrasaurus") Display(dinosaurs) Console.WriteLine(vbLf & "IndexOf(""Muttaburrasaurus""): {0}", _ dinosaurs.IndexOf("Muttaburrasaurus")) Console.WriteLine(vbLf & "Contains(""Caudipteryx""): {0}", _ dinosaurs.Contains("Caudipteryx")) Console.WriteLine(vbLf & "Insert(2, ""Nanotyrannus"")") dinosaurs.Insert(2, "Nanotyrannus") Console.WriteLine(vbLf & "dinosaurs(2): {0}", dinosaurs(2)) Console.WriteLine(vbLf & "dinosaurs(2) = ""Microraptor""") dinosaurs(2) = "Microraptor" Console.WriteLine(vbLf & "Remove(""Microraptor"")") dinosaurs.Remove("Microraptor") Console.WriteLine(vbLf & "RemoveAt(0)") dinosaurs.RemoveAt(0) Display(dinosaurs) End Sub Private Shared Sub Display(ByVal cs As Collection(Of String)) Console.WriteLine() For Each item As String In cs Console.WriteLine(item) Next item End Sub Private Shared Sub ChangedHandler(ByVal source As Object, _ ByVal e As DinosaursChangedEventArgs) If e.ChangeType = ChangeType.Replaced Then Console.WriteLine("{0} was replaced with {1}", _ e.ChangedItem, e.ReplacedWith) ElseIf e.ChangeType = ChangeType.Cleared Then Console.WriteLine("The dinosaur list was cleared.") Else Console.WriteLine("{0} was {1}.", _ e.ChangedItem, e.ChangeType) End If End Sub End Class ' This code example produces the following output: ' 'Psitticosaurus was Added. 'Caudipteryx was Added. 'Compsognathus was Added. 'Muttaburrasaurus was Added. ' 'Psitticosaurus 'Caudipteryx 'Compsognathus 'Muttaburrasaurus ' 'IndexOf("Muttaburrasaurus"): 3 ' 'Contains("Caudipteryx"): True ' 'Insert(2, "Nanotyrannus") 'Nanotyrannus was Added. ' 'dinosaurs(2): Nanotyrannus ' 'dinosaurs(2) = "Microraptor" 'Nanotyrannus was replaced with Microraptor ' 'Remove("Microraptor") 'Microraptor was Removed. ' 'RemoveAt(0) 'Psitticosaurus was Removed. ' 'Caudipteryx 'Compsognathus 'Muttaburrasaurus
using System; using System.Collections.Generic; using System.Collections.ObjectModel; public class Dinosaurs : Collection<string> { public event EventHandler<DinosaursChangedEventArgs> Changed; protected override void InsertItem(int index, string newItem) { base.InsertItem(index, newItem); EventHandler<DinosaursChangedEventArgs> temp = Changed; if (temp != null) { temp(this, new DinosaursChangedEventArgs( ChangeType.Added, newItem, null)); } } protected override void SetItem(int index, string newItem) { string replaced = Items[index]; base.SetItem(index, newItem); EventHandler<DinosaursChangedEventArgs> temp = Changed; if (temp != null) { temp(this, new DinosaursChangedEventArgs( ChangeType.Replaced, replaced, newItem)); } } protected override void RemoveItem(int index) { string removedItem = Items[index]; base.RemoveItem(index); EventHandler<DinosaursChangedEventArgs> temp = Changed; if (temp != null) { temp(this, new DinosaursChangedEventArgs( ChangeType.Removed, removedItem, null)); } } protected override void ClearItems() { base.ClearItems(); EventHandler<DinosaursChangedEventArgs> temp = Changed; if (temp != null) { temp(this, new DinosaursChangedEventArgs( ChangeType.Cleared, null, null)); } } } // Event argument for the Changed event. // public class DinosaursChangedEventArgs : EventArgs { public readonly string ChangedItem; public readonly ChangeType ChangeType; public readonly string ReplacedWith; public DinosaursChangedEventArgs(ChangeType change, string item, string replacement) { ChangeType = change; ChangedItem = item; ReplacedWith = replacement; } } public enum ChangeType { Added, Removed, Replaced, Cleared }; public class Demo { public static void Main() { Dinosaurs dinosaurs = new Dinosaurs(); dinosaurs.Changed += ChangedHandler; dinosaurs.Add("Psitticosaurus"); dinosaurs.Add("Caudipteryx"); dinosaurs.Add("Compsognathus"); dinosaurs.Add("Muttaburrasaurus"); Display(dinosaurs); Console.WriteLine("\nIndexOf(\"Muttaburrasaurus\"): {0}", dinosaurs.IndexOf("Muttaburrasaurus")); Console.WriteLine("\nContains(\"Caudipteryx\"): {0}", dinosaurs.Contains("Caudipteryx")); Console.WriteLine("\nInsert(2, \"Nanotyrannus\")"); dinosaurs.Insert(2, "Nanotyrannus"); Console.WriteLine("\ndinosaurs[2]: {0}", dinosaurs[2]); Console.WriteLine("\ndinosaurs[2] = \"Microraptor\""); dinosaurs[2] = "Microraptor"; Console.WriteLine("\nRemove(\"Microraptor\")"); dinosaurs.Remove("Microraptor"); Console.WriteLine("\nRemoveAt(0)"); dinosaurs.RemoveAt(0); Display(dinosaurs); } private static void Display(Collection<string> cs) { Console.WriteLine(); foreach( string item in cs ) { Console.WriteLine(item); } } private static void ChangedHandler(object source, DinosaursChangedEventArgs e) { if (e.ChangeType==ChangeType.Replaced) { Console.WriteLine("{0} was replaced with {1}", e.ChangedItem, e.ReplacedWith); } else if(e.ChangeType==ChangeType.Cleared) { Console.WriteLine("The dinosaur list was cleared."); } else { Console.WriteLine("{0} was {1}.", e.ChangedItem, e.ChangeType); } } } /* This code example produces the following output: Psitticosaurus was Added. Caudipteryx was Added. Compsognathus was Added. Muttaburrasaurus was Added. Psitticosaurus Caudipteryx Compsognathus Muttaburrasaurus IndexOf("Muttaburrasaurus"): 3 Contains("Caudipteryx"): True Insert(2, "Nanotyrannus") Nanotyrannus was Added. dinosaurs[2]: Nanotyrannus dinosaurs[2] = "Microraptor" Nanotyrannus was replaced with Microraptor Remove("Microraptor") Microraptor was Removed. RemoveAt(0) Psitticosaurus was Removed. Caudipteryx Compsognathus Muttaburrasaurus */


この型の public static (Visual Basic では Shared) メンバは、スレッド セーフです。すべてのインスタンス メンバがスレッド セーフになるかどうかは保証されていません。
コレクションが変更されない限り、Collection では、複数の読み込み操作が同時に発生しても問題ありません。ただし、コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。すべての列挙処理が終わるまでコレクションをロックすることにより、列挙処理でのスレッド セーフを確保できます。コレクションに対し複数のスレッドがアクセスして読み取りや書き込みを行うことができるようにするには、独自に同期化を実装する必要があります。

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Collection プロパティ


関連項目
Collection クラスMicrosoft.VisualBasic 名前空間
その他の技術情報
Collection オブジェクト (Visual Basic)Collection オブジェクトのメンバ
GetEnumerator メソッド (コレクション オブジェクト)
Collection プロパティ
Collection メソッド

名前 | 説明 | |
---|---|---|
![]() | Add | 要素を Collection オブジェクトに追加します。 |
![]() | Clear | Visual Basic の Collection オブジェクトの要素をすべて削除します。 |
![]() | Contains | Visual Basic の Collection オブジェクトに特定のキーを持つ要素が含まれているかどうかを示す Boolean 値を返します。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetEnumerator | Collection オブジェクト (Visual Basic) に対する反復処理に使用する、列挙子オブジェクトへの参照を返します。 |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | Remove | オーバーロードされます。 Collection オブジェクトから要素を削除します。 |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |

関連項目
Collection クラスMicrosoft.VisualBasic 名前空間
その他の技術情報
Collection オブジェクト (Visual Basic)Collection オブジェクトのメンバ
GetEnumerator メソッド (コレクション オブジェクト)
Collection メソッド

名前 | 説明 | |
---|---|---|
![]() | Add | Collection の末尾にオブジェクトを追加します。 |
![]() | Clear | Collection からすべての要素を削除します。 |
![]() | Contains | ある要素が Collection 内に存在するかどうかを判断します。 |
![]() | CopyTo | Collection 全体を互換性のある 1 次元の Array にコピーします。コピー操作は、コピー先の配列の指定したインデックスから始まります。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetEnumerator | Collection を反復処理する列挙子を返します。 |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | IndexOf | 指定したオブジェクトを検索し、Collection 全体内で最初に見つかった位置の 0 から始まるインデックスを返します。 |
![]() | Insert | Collection 内の指定したインデックスの位置に要素を挿入します。 |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | Remove | Collection 内で最初に見つかった特定のオブジェクトを削除します。 |
![]() | RemoveAt | Collection の指定したインデックスにある要素を削除します。 |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | ClearItems | Collection からすべての要素を削除します。 |
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | InsertItem | Collection 内の指定したインデックスの位置に要素を挿入します。 |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |
![]() | RemoveItem | Collection の指定したインデックスにある要素を削除します。 |
![]() | SetItem | 指定したインデックス位置にある要素を置き換えます。 |

名前 | 説明 | |
---|---|---|
![]() | System.Collections.ICollection.CopyTo | ICollection の要素を Array にコピーします。Array の特定のインデックスからコピーが開始されます。 |
![]() | System.Collections.IEnumerable.GetEnumerator | コレクションを反復処理する列挙子を返します。 |
![]() | System.Collections.IList.Add | IList に項目を追加します。 |
![]() | System.Collections.IList.Contains | IList に特定の値が格納されているかどうかを判断します。 |
![]() | System.Collections.IList.IndexOf | IList 内での指定した項目のインデックスを調べます。 |
![]() | System.Collections.IList.Insert | IList 内の指定したインデックスの位置に項目を挿入します。 |
![]() | System.Collections.IList.Remove | IList 内で最初に見つかった特定のオブジェクトを削除します。 |

Collection メンバ
Visual Basic の Collection は、一定の順序で並んだ項目のセットであり、1 つの単位として参照できます。
Collection データ型で公開されるメンバを以下の表に示します。

名前 | 説明 | |
---|---|---|
![]() | Collection | 新しい Visual Basic の Collection オブジェクト (Visual Basic) を作成して返します。 |


名前 | 説明 | |
---|---|---|
![]() | Add | 要素を Collection オブジェクトに追加します。 |
![]() | Clear | Visual Basic の Collection オブジェクトの要素をすべて削除します。 |
![]() | Contains | Visual Basic の Collection オブジェクトに特定のキーを持つ要素が含まれているかどうかを示す Boolean 値を返します。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetEnumerator | Collection オブジェクト (Visual Basic) に対する反復処理に使用する、列挙子オブジェクトへの参照を返します。 |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | Remove | オーバーロードされます。 Collection オブジェクトから要素を削除します。 |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |

関連項目
Collection クラスMicrosoft.VisualBasic 名前空間
その他の技術情報
Collection オブジェクト (Visual Basic)Collection オブジェクトのメンバ
GetEnumerator メソッド (コレクション オブジェクト)
Collection メンバ
Collection ジェネリック型で公開されるメンバを以下の表に示します。




名前 | 説明 | |
---|---|---|
![]() | Add | Collection の末尾にオブジェクトを追加します。 |
![]() | Clear | Collection からすべての要素を削除します。 |
![]() | Contains | ある要素が Collection 内に存在するかどうかを判断します。 |
![]() | CopyTo | Collection 全体を互換性のある 1 次元の Array にコピーします。コピー操作は、コピー先の配列の指定したインデックスから始まります。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetEnumerator | Collection を反復処理する列挙子を返します。 |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | IndexOf | 指定したオブジェクトを検索し、Collection 全体内で最初に見つかった位置の 0 から始まるインデックスを返します。 |
![]() | Insert | Collection 内の指定したインデックスの位置に要素を挿入します。 |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | Remove | Collection 内で最初に見つかった特定のオブジェクトを削除します。 |
![]() | RemoveAt | Collection の指定したインデックスにある要素を削除します。 |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | ClearItems | Collection からすべての要素を削除します。 |
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | InsertItem | Collection 内の指定したインデックスの位置に要素を挿入します。 |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |
![]() | RemoveItem | Collection の指定したインデックスにある要素を削除します。 |
![]() | SetItem | 指定したインデックス位置にある要素を置き換えます。 |

名前 | 説明 | |
---|---|---|
![]() | System.Collections.ICollection.CopyTo | ICollection の要素を Array にコピーします。Array の特定のインデックスからコピーが開始されます。 |
![]() | System.Collections.IEnumerable.GetEnumerator | コレクションを反復処理する列挙子を返します。 |
![]() | System.Collections.IList.Add | IList に項目を追加します。 |
![]() | System.Collections.IList.Contains | IList に特定の値が格納されているかどうかを判断します。 |
![]() | System.Collections.IList.IndexOf | IList 内での指定した項目のインデックスを調べます。 |
![]() | System.Collections.IList.Insert | IList 内の指定したインデックスの位置に項目を挿入します。 |
![]() | System.Collections.IList.Remove | IList 内で最初に見つかった特定のオブジェクトを削除します。 |
![]() | System.Collections.IList.Item | 指定したインデックスにある要素を取得または設定します。 |

コレクション (曖昧さ回避)
(collection から転送)
出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2022/06/02 08:06 UTC 版)
コレクション(Collection)は、ものを広く収集すること、あるいは収集された物・作品群のこと。とくに美術館や博物館などの収蔵品群をコレクションという。
- 1 コレクション (曖昧さ回避)とは
- 2 コレクション (曖昧さ回避)の概要
COLLECTION
出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2019/11/23 14:40 UTC 版)
『COLLECTION』(コレクション)は、中山美穂の初のコレクション・アルバム(シングル・コレクション)。1987年11月15日にキングレコードより発売された(K32X-200)。
|
- 1 COLLECTIONとは
- 2 COLLECTIONの概要
固有名詞の分類
- collectionのページへのリンク