ReadOnlyCollectionBase.GetEnumerator メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > ReadOnlyCollectionBase.GetEnumerator メソッドの意味・解説 

ReadOnlyCollectionBase.GetEnumerator メソッド

ReadOnlyCollectionBase インスタンス反復処理する列挙子を返します

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Overridable Function
 GetEnumerator As IEnumerator
Dim instance As ReadOnlyCollectionBase
Dim returnValue As IEnumerator

returnValue = instance.GetEnumerator

戻り値
ReadOnlyCollectionBase インスタンスの IEnumerator。

解説解説

C# 言語foreach ステートメント (Visual Basic の場合for each) を使用することで列挙子の複雑さ回避できます。したがって列挙子を直接操作するではなくforeach使用お勧めます。

列挙子を使用すると、コレクション内のデータ読み取ることができますが、基になるコレクション変更することはできません。

初期状態では、列挙子はコレクション最初要素前に位置してます。Reset実行した場合も、列挙子はこの位置に戻されます。この位置で、Current未定義です。したがってCurrent の値を読み取る前に、MoveNext を呼び出してコレクション最初要素列挙子を進める必要があります

Current は、MoveNext または Reset呼び出されるまでは同じオブジェクト返しますMoveNext は、Current次の要素設定します

MoveNextコレクション末尾を過ぎると、列挙子はコレクション最後要素後ろ配置されMoveNextfalse返します列挙子がこの位置にある場合以降MoveNext呼び出してfalse返されます。MoveNext への最後呼び出しfalse返され場合は、Current未定義です。Currentコレクション最初要素に再び設定するには、Reset呼び出してから、MoveNext呼び出します。

列挙子は、コレクション変更されない限り有効です。要素追加変更削除などの変更コレクションに対して実行されると、列挙子は回復不可能な無効状態になり、動作未定義になります

列挙子はコレクションへの排他アクセス権持たないため、コレクション列挙処理は、本質的にスレッド セーフな処理ではありません。すべての列挙処理が終わるまでコレクションロックすることにより、列挙処理でのスレッド セーフ確保できますコレクション対し複数スレッドアクセスして読み取り書き込みを行うことができるようにするには、独自に同期化実装する必要があります

このメソッドは O(1) 操作です。

使用例使用例

ReadOnlyCollectionBase クラス実装するコード例次に示します

Imports System
Imports System.Collections

Public Class ROCollection
    Inherits ReadOnlyCollectionBase


    Public Sub New(sourceList
 As IList)
        InnerList.AddRange(sourceList)
    End Sub 'New


    Default Public ReadOnly
 Property Item(index As Integer)
 As [Object]
        Get
            Return InnerList(index)
        End Get
    End Property


    Public Function IndexOf(value As
 [Object]) As Integer
        Return InnerList.IndexOf(value)
    End Function 'IndexOf


    Public Function Contains(value As
 [Object]) As Boolean
        Return InnerList.Contains(value)
    End Function 'Contains

End Class 'ROCollection 


Public Class SamplesCollectionBase

    Public Shared Sub Main()

        ' Create an ArrayList.
        Dim myAL As New
 ArrayList()
        myAL.Add("red")
        myAL.Add("blue")
        myAL.Add("yellow")
        myAL.Add("green")
        myAL.Add("orange")
        myAL.Add("purple")

        ' Create a new ROCollection that contains the elements in myAL.
        Dim myCol As New
 ROCollection(myAL)

        ' Display the contents of the collection using For Each. This
 is the preferred method.
        Console.WriteLine("Contents of the collection (using For
 Each):")
        PrintValues1(myCol)

        ' Display the contents of the collection using the enumerator.
        Console.WriteLine("Contents of the collection (using enumerator):")
        PrintValues2(myCol)

        ' Display the contents of the collection using the Count property
 and the Item property.
        Console.WriteLine("Contents of the collection (using Count
 and Item):")
        PrintIndexAndValues(myCol)

        ' Search the collection with Contains and IndexOf.
        Console.WriteLine("Contains yellow: {0}",
 myCol.Contains("yellow"))
        Console.WriteLine("orange is at index {0}.",
 myCol.IndexOf("orange"))
        Console.WriteLine()

    End Sub 'Main


    ' Uses the Count property and the Item property.
    Public Shared Sub PrintIndexAndValues(myCol
 As ROCollection)
        Dim i As Integer
        For i = 0 To myCol.Count - 1
            Console.WriteLine("   [{0}]:   {1}", i,
 myCol(i))
        Next i
        Console.WriteLine()
    End Sub 'PrintIndexAndValues


    ' Uses the For Each statement which hides the complexity of the
 enumerator.
    ' NOTE: The For Each statement is the preferred way of enumerating
 the contents of a collection.
    Public Shared Sub PrintValues1(myCol
 As ROCollection)
        Dim obj As [Object]
        For Each obj In
  myCol
            Console.WriteLine("   {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub 'PrintValues1


    ' Uses the enumerator. 
    ' NOTE: The For Each statement is the preferred way of enumerating
 the contents of a collection.
    Public Shared Sub PrintValues2(myCol
 As ROCollection)
        Dim myEnumerator As System.Collections.IEnumerator
 = myCol.GetEnumerator()
        While myEnumerator.MoveNext()
            Console.WriteLine("   {0}", myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub 'PrintValues2

End Class 'SamplesCollectionBase
 


'This code produces the following output.
'
'Contents of the collection (using For Each):
'   red
'   blue
'   yellow
'   green
'   orange
'   purple
'
'Contents of the collection (using enumerator):
'   red
'   blue
'   yellow
'   green
'   orange
'   purple
'
'Contents of the collection (using Count and Item):
'   [0]:   red
'   [1]:   blue
'   [2]:   yellow
'   [3]:   green
'   [4]:   orange
'   [5]:   purple
'
'Contains yellow: True
'orange is at index 4.

using System;
using System.Collections;

public class ROCollection : ReadOnlyCollectionBase
  {

   public ROCollection( IList sourceList )  {
      InnerList.AddRange( sourceList );
   }

   public Object this[ int
 index ]  {
      get  {
         return( InnerList[index] );
      }
   }

   public int IndexOf( Object value )  {
      return( InnerList.IndexOf( value ) );
   }

   public bool Contains( Object value )  {
      return( InnerList.Contains( value ) );
   }

}


public class SamplesCollectionBase  {

   public static void Main()
  {

      // Create an ArrayList.
      ArrayList myAL = new ArrayList();
      myAL.Add( "red" );
      myAL.Add( "blue" );
      myAL.Add( "yellow" );
      myAL.Add( "green" );
      myAL.Add( "orange" );
      myAL.Add( "purple" );
 
      // Create a new ROCollection that contains the elements in myAL.
      ROCollection myCol = new ROCollection( myAL );

      // Display the contents of the collection using foreach. This
 is the preferred method.
      Console.WriteLine( "Contents of the collection (using
 foreach):" );
      PrintValues1( myCol );

      // Display the contents of the collection using the enumerator.
      Console.WriteLine( "Contents of the collection (using
 enumerator):" );
      PrintValues2( myCol );

      // Display the contents of the collection using the Count property
 and the Item property.
      Console.WriteLine( "Contents of the collection (using
 Count and Item):" );
      PrintIndexAndValues( myCol );

      // Search the collection with Contains and IndexOf.
      Console.WriteLine( "Contains yellow: {0}", myCol.Contains( "yellow"
 ) );
      Console.WriteLine( "orange is at index {0}.", myCol.IndexOf( "orange"
 ) );
      Console.WriteLine();

   }
 
   // Uses the Count property and the Item property.
   public static void PrintIndexAndValues(
 ROCollection myCol )  {
      for ( int i = 0; i < myCol.Count;
 i++ )
         Console.WriteLine( "   [{0}]:   {1}", i, myCol[i] );
      Console.WriteLine();
   }

   // Uses the foreach statement which hides the complexity of the enumerator.
   // NOTE: The foreach statement is the preferred way of enumerating
 the contents of a collection.
   public static void PrintValues1(
 ROCollection myCol )  {
      foreach ( Object obj in myCol )
         Console.WriteLine( "   {0}", obj );
      Console.WriteLine();
   }

   // Uses the enumerator. 
   // NOTE: The foreach statement is the preferred way of enumerating
 the contents of a collection.
   public static void PrintValues2(
 ROCollection myCol )  {
      System.Collections.IEnumerator myEnumerator = myCol.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "   {0}", myEnumerator.Current );
      Console.WriteLine();
   }

}


/* 
This code produces the following output.

Contents of the collection (using foreach):
   red
   blue
   yellow
   green
   orange
   purple

Contents of the collection (using enumerator):
   red
   blue
   yellow
   green
   orange
   purple

Contents of the collection (using Count and Item):
   [0]:   red
   [1]:   blue
   [2]:   yellow
   [3]:   green
   [4]:   orange
   [5]:   purple

Contains yellow: True
orange is at index 4.

*/

using namespace System;
using namespace System::Collections;
public ref class ROCollection: public
 ReadOnlyCollectionBase
{
public:
   ROCollection( IList^ sourceList )
   {
      InnerList->AddRange( sourceList );
   }

   property Object^ Item [int]
   {
      Object^ get( int index )
      {
         return (InnerList[ index ]);
      }

   }
   int IndexOf( Object^ value )
   {
      return (InnerList->IndexOf( value ));
   }

   bool Contains( Object^ value )
   {
      return (InnerList->Contains( value ));
   }

};

void PrintIndexAndValues( ROCollection^ myCol );
void PrintValues2( ROCollection^ myCol );
int main()
{
   // Create an ArrayList.
   ArrayList^ myAL = gcnew ArrayList;
   myAL->Add( "red" );
   myAL->Add( "blue" );
   myAL->Add( "yellow" );
   myAL->Add( "green" );
   myAL->Add( "orange" );
   myAL->Add( "purple" );

   // Create a new ROCollection that contains the elements in myAL.
   ROCollection^ myCol = gcnew ROCollection( myAL );

   // Display the contents of the collection using the enumerator.
   Console::WriteLine( "Contents of the collection (using
 enumerator):" );
   PrintValues2( myCol );

   // Display the contents of the collection using the Count property
 and the Item property.
   Console::WriteLine( "Contents of the collection (using
 Count and Item):" );
   PrintIndexAndValues( myCol );

   // Search the collection with Contains and IndexOf.
   Console::WriteLine( "Contains yellow: {0}", myCol->Contains( "yellow"
 ) );
   Console::WriteLine( "orange is at index {0}.", myCol->IndexOf( "orange"
 ) );
   Console::WriteLine();
}


// Uses the Count property and the Item property.
void PrintIndexAndValues( ROCollection^ myCol )
{
   for ( int i = 0; i < myCol->Count;
 i++ )
      Console::WriteLine( "   [{0}]:   {1}", i, myCol->Item[ i ] );
   Console::WriteLine();
}


// Uses the enumerator. 
void PrintValues2( ROCollection^ myCol )
{
   System::Collections::IEnumerator^ myEnumerator = myCol->GetEnumerator();
   while ( myEnumerator->MoveNext() )
      Console::WriteLine( "   {0}", myEnumerator->Current );

   Console::WriteLine();
}

/* 
This code produces the following output.

Contents of the collection (using enumerator):
   red
   blue
   yellow
   green
   orange
   purple

Contents of the collection (using Count and Item):
   [0]:   red
   [1]:   blue
   [2]:   yellow
   [3]:   green
   [4]:   orange
   [5]:   purple

Contains yellow: True
orange is at index 4.

*/
import System.*;
import System.Collections.*;

public class ROCollection extends ReadOnlyCollectionBase
{
    public ROCollection(IList sourceList) 
    {
        get_InnerList().AddRange(sourceList);
    } //ROCollection
   
    /** @property 
     */
    public Object get_Item(int index)
    {
        return get_InnerList().get_Item(index);
    } //get_Item
     
    public int IndexOf(Object value) 
    {
        return get_InnerList().IndexOf(value);
    } //IndexOf
   
    public boolean Contains(Object value) 
    {
        return get_InnerList().Contains(value);
    } //Contains
} //ROCollection

public class SamplesCollectionBase
{
    public static void main(String[]
 args)
    {
        // Create an ArrayList.
        ArrayList myAL = new ArrayList();
        myAL.Add("red");
        myAL.Add("blue");
        myAL.Add("yellow");
        myAL.Add("green");
        myAL.Add("orange");
        myAL.Add("purple");
          
        // Create a new ROCollection that contains the elements in myAL.
        ROCollection myCol = new ROCollection(myAL);
          
        // Display the contents of the collection using for. This is
 the 
        // preferred method.
        Console.WriteLine("Contents of the collection (using
 for):");
        PrintValues1(myCol);
          
        // Display the contents of the collection using the enumerator.
        Console.WriteLine("Contents of the collection (using
 enumerator):");
        PrintValues2(myCol);
          
        // Display the contents of the collection using the Count property
 and 
        // the Item property.
        Console.WriteLine("Contents of the collection (using
 Count and Item):");
        PrintIndexAndValues(myCol);
          
        // Search the collection with Contains and IndexOf.
        Console.WriteLine("Contains yellow: {0}",
            (System.Boolean)myCol.Contains("yellow"));
        Console.WriteLine("orange is at index {0}.", 
            (Int32)myCol.IndexOf("orange"));
        Console.WriteLine();
    } //main
    
    // Uses the Count property and the Item property.
    public static void PrintIndexAndValues(ROCollection
 myCol) 
    {
        for(int i = 0; i < myCol.get_Count();
 i++) {
            Console.WriteLine("   [{0}]:   {1}",(Int32)i, myCol.get_Item(i));
        } 
        Console.WriteLine();
    } //PrintIndexAndValues
   
    // Uses the for statement which hides the complexity of the enumerator.
    // NOTE: The for statement is the preferred way of enumerating the
 contents
    // of a collection.
    public static void PrintValues1(ROCollection
 myCol) 
    {
        for (int iCtr = 0; iCtr < myCol.get_Count();
 iCtr++ ) {
            Object obj = myCol.get_Item(iCtr);
            Console.WriteLine("   {0}", obj);
        }
        Console.WriteLine();
    } //PrintValues1
     
    // Uses the enumerator. 
    // NOTE: The for statement is the preferred way of enumerating the
    // contents of a collection.
    public static void PrintValues2(ROCollection
 myCol) 
    {
        System.Collections.IEnumerator myEnumerator = myCol.GetEnumerator();
        while(myEnumerator.MoveNext()) {
            Console.WriteLine("   {0}", myEnumerator.get_Current());
        }
        Console.WriteLine();
    } //PrintValues2
} //SamplesCollectionBase
 
/* 
This code produces the following output.

Contents of the collection (using for):
   red
   blue
   yellow
   green
   orange
   purple

Contents of the collection (using enumerator):
   red
   blue
   yellow
   green
   orange
   purple

Contents of the collection (using Count and Item):
   [0]:   red
   [1]:   blue
   [2]:   yellow
   [3]:   green
   [4]:   orange
   [5]:   purple

Contains yellow: True
orange is at index 4.

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ReadOnlyCollectionBase クラス
ReadOnlyCollectionBase メンバ
System.Collections 名前空間
IEnumerator インターフェイス


このページでは「.NET Framework クラス ライブラリ リファレンス」からReadOnlyCollectionBase.GetEnumerator メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からReadOnlyCollectionBase.GetEnumerator メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からReadOnlyCollectionBase.GetEnumerator メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

ReadOnlyCollectionBase.GetEnumerator メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ReadOnlyCollectionBase.GetEnumerator メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS