Stack.GetEnumerator メソッド
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As Stack Dim returnValue As IEnumerator returnValue = instance.GetEnumerator
public virtual IEnumerator GetEnumerator ()
public: virtual IEnumerator^ GetEnumerator ()
public IEnumerator GetEnumerator ()
public function GetEnumerator () : IEnumerator
Stack の IEnumerator。

C# 言語の foreach ステートメント (Visual Basic の場合は for each) を使用することで列挙子の複雑さを回避できます。したがって、列挙子を直接操作するのではなく、foreach の使用をお勧めします。
列挙子を使用すると、コレクション内のデータを読み取ることができますが、基になるコレクションを変更することはできません。
初期状態では、列挙子はコレクションの最初の要素の前に位置しています。Reset を実行した場合も、列挙子はこの位置に戻されます。この位置で、Current が未定義です。したがって、Current の値を読み取る前に、MoveNext を呼び出して、コレクションの最初の要素に列挙子を進める必要があります。
Current は、MoveNext または Reset が呼び出されるまでは同じオブジェクトを返します。MoveNext は、Current を次の要素に設定します。
MoveNext がコレクションの末尾を過ぎると、列挙子はコレクションの最後の要素の後ろに配置され、MoveNext は false を返します。列挙子がこの位置にある場合、以降、MoveNext を呼び出しても false が返されます。MoveNext への最後の呼び出しで false が返された場合は、Current が未定義です。Current をコレクションの最初の要素に再び設定するには、Reset を呼び出してから、MoveNext を呼び出します。
列挙子は、コレクションが変更されない限り有効です。要素の追加、変更、削除などの変更がコレクションに対して実行されると、列挙子は回復不可能な無効状態になり、動作は未定義になります。
列挙子はコレクションへの排他アクセス権を持たないため、コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。すべての列挙処理が終わるまでコレクションをロックすることにより、列挙処理でのスレッド セーフを確保できます。コレクションに対し複数のスレッドがアクセスして読み取りや書き込みを行うことができるようにするには、独自に同期化を実装する必要があります。

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Stack.GetEnumerator メソッド
アセンブリ: System (system.dll 内)

Dim instance As Stack(Of T) Dim returnValue As Enumerator returnValue = instance.GetEnumerator
public Enumerator GetEnumerator ()
public: Enumerator GetEnumerator ()
public Enumerator GetEnumerator ()
public function GetEnumerator () : Enumerator
Stack の Stack.Enumerator。

C# 言語の foreach ステートメント (C++ の場合は for each、Visual Basic の場合は For Each) を使用することで列挙子の複雑さを回避できます。したがって、列挙子を直接操作するのではなく、foreach の使用をお勧めします。
列挙子を使用すると、コレクション内のデータを読み取ることができますが、基になるコレクションを変更することはできません。
最初に、列挙子はコレクションの最初の要素の前に配置されます。この位置では、Current が未定義です。したがって、Current の値を読み取る前に、MoveNext を呼び出して、コレクションの最初の要素に列挙子を進める必要があります。
Current は、MoveNext が呼び出されるまでは同じオブジェクトを返します。MoveNext は、Current を次の要素に設定します。
MoveNext がコレクションの末尾を過ぎると、列挙子の位置はコレクションの最後の要素の後ろとなり、MoveNext は false を返します。列挙子がこの位置にある場合、以降、MoveNext を呼び出しても false が返されます。MoveNext への最後の呼び出しで false が返された場合は、Current が未定義です。Current を、再度、コレクションの最初の要素に設定することはできません。列挙子の新しいインスタンスを作成する必要があります。
コレクションが変更されない限り、列挙子は有効なままです。要素の追加、変更、削除などの変更がコレクションに対して実行されると、列挙子は回復不可能な無効状態になり、動作は未定義になります。
列挙子はコレクションへの排他アクセス権を持たないため、コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。すべての列挙処理が終わるまでコレクションをロックすることにより、列挙処理でのスレッド セーフを確保できます。コレクションに対し複数のスレッドがアクセスして読み取りや書き込みを行うことができるようにするには、独自に同期化を実装する必要があります。

Stack ジェネリック クラスが列挙可能であることを示すコード例を次に示します。foreach ステートメント (Visual Basic の場合は For Each、C++ の場合は for each) を使用してキューを列挙します。
このコード例では、既定の容量で文字列のスタックを作成し、Push メソッドを使用して 5 つの文字列をスタックにプッシュします。スタックの要素は列挙されますが、スタックの状態は変化しません。Pop メソッドを使用して、最初の文字列をスタックからポップします。Peek メソッドを使用してスタック内の次の項目を調べてから、Pop メソッドを使用してその項目をスタックからポップします。
ToArray メソッドを使用して配列を作成し、その配列にスタック要素をコピーしてから、IEnumerable を受け取る Stack コンストラクタに配列を渡して、要素の順序を反転したスタックのコピーを作成します。コピーの要素が表示されます。
スタックの 2 倍のサイズの配列が作成され、CopyTo メソッドを使用して配列の中央部で始まる配列要素をコピーします。Stack コンストラクタを再度使用して、要素の順序を反転したスタックのコピーを作成します。その結果、3 つの null 要素が末尾に配置されます。
Contains メソッドを使用して、文字列 "four" がスタックの最初のコピーにあることを示します。その後で、Clear メソッドがコピーをクリアすると、Count プロパティによってスタックが空であることが示されます。
Imports System Imports System.Collections.Generic Module Example Sub Main Dim numbers As New Stack(Of String) numbers.Push("one") numbers.Push("two") numbers.Push("three") numbers.Push("four") numbers.Push("five") ' A stack can be enumerated without disturbing its contents. For Each number As String In numbers Console.WriteLine(number) Next Console.WriteLine(vbLf & "Popping '{0}'", numbers.Pop()) Console.WriteLine("Peek at next item to pop: {0}", _ numbers.Peek()) Console.WriteLine("Popping '{0}'", numbers.Pop()) ' Create another stack, using the ToArray method and the ' constructor that accepts an IEnumerable(Of T). Note that ' the order of items on the new stack is reversed. Dim stack2 As New Stack(Of String)(numbers.ToArray()) Console.WriteLine(vbLf & "Contents of the first copy:") For Each number As String In stack2 Console.WriteLine(number) Next ' Create an array twice the size of the stack, compensating ' for the fact that Visual Basic allocates an extra array ' element. Copy the elements of the stack, starting at the ' middle of the array. Dim array2((numbers.Count * 2) - 1) As String numbers.CopyTo(array2, numbers.Count) ' Create a second stack, using the constructor that accepts an ' IEnumerable(Of T). The elements are reversed, with the null ' elements appearing at the end of the stack when enumerated. Dim stack3 As New Stack(Of String)(array2) Console.WriteLine(vbLf & _ "Contents of the second copy, with duplicates and nulls:") For Each number As String In stack3 Console.WriteLine(number) Next Console.WriteLine(vbLf & "stack2.Contains(""four"") = {0}", _ stack2.Contains("four")) Console.WriteLine(vbLf & "stack2.Clear()") stack2.Clear() Console.WriteLine(vbLf & "stack2.Count = {0}", _ stack2.Count) End Sub End Module ' This code example produces the following output: ' 'five 'four 'three 'two 'one ' 'Popping 'five' 'Peek at next item to pop: four 'Popping 'four' ' 'Contents of the first copy: 'one 'two 'three ' 'Contents of the second copy, with duplicates and nulls: 'one 'two 'three ' ' ' ' 'stack2.Contains("four") = False ' 'stack2.Clear() ' 'stack2.Count = 0
using System; using System.Collections.Generic; class Example { public static void Main() { Stack<string> numbers = new Stack<string>(); numbers.Push("one"); numbers.Push("two"); numbers.Push("three"); numbers.Push("four"); numbers.Push("five"); // A stack can be enumerated without disturbing its contents. foreach( string number in numbers ) { Console.WriteLine(number); } Console.WriteLine("\nPopping '{0}'", numbers.Pop()); Console.WriteLine("Peek at next item to destack: {0}", numbers.Peek()); Console.WriteLine("Popping '{0}'", numbers.Pop()); // Create a copy of the stack, using the ToArray method and the // constructor that accepts an IEnumerable<T>. Stack<string> stack2 = new Stack<string>(numbers.ToArray()); Console.WriteLine("\nContents of the first copy:"); foreach( string number in stack2 ) { Console.WriteLine(number); } // Create an array twice the size of the stack and copy the // elements of the stack, starting at the middle of the // array. string[] array2 = new string[numbers.Count * 2]; numbers.CopyTo(array2, numbers.Count); // Create a second stack, using the constructor that accepts an // IEnumerable(Of T). Stack<string> stack3 = new Stack<string>(array2); Console.WriteLine("\nContents of the second copy, with duplicates and nulls:"); foreach( string number in stack3 ) { Console.WriteLine(number); } Console.WriteLine("\nstack2.Contains(\"four\") = {0}", stack2.Contains("four")); Console.WriteLine("\nstack2.Clear()"); stack2.Clear(); Console.WriteLine("\nstack2.Count = {0}", stack2.Count); } } /* This code example produces the following output: five four three two one Popping 'five' Peek at next item to destack: four Popping 'four' Contents of the first copy: one two three Contents of the second copy, with duplicates and nulls: one two three stack2.Contains("four") = False stack2.Clear() stack2.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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Stack ジェネリック クラス
Stack メンバ
System.Collections.Generic 名前空間
Stack.Enumerator
IEnumerator ジェネリック インターフェイス
- Stack.GetEnumeratorのページへのリンク