ReadOnlyCollectionBase.GetEnumerator Yöntem

Tanım

Örnekte yineleyen ReadOnlyCollectionBase bir numaralandırıcı döndürür.

C#
public virtual System.Collections.IEnumerator GetEnumerator ();
C#
public System.Collections.IEnumerator GetEnumerator ();

Döndürülenler

Örnek için ReadOnlyCollectionBase birIEnumerator.

Uygulamalar

Örnekler

Aşağıdaki kod örneği sınıfını ReadOnlyCollectionBase uygular.

C#
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.

*/

Açıklamalar

foreach C# dilinin deyimi (for eachVisual Basic'te) numaralandırıcıların karmaşıklığını gizler. Bu nedenle, numaralandırıcıyı doğrudan işlemek yerine kullanılması foreach önerilir.

Numaralandırıcılar, koleksiyonundaki verileri okumak için kullanılabilir, ancak temel koleksiyonu değiştirmek için kullanılamaz.

Başlangıçta, numaralandırıcı, koleksiyondaki ilk öğenin önüne yerleştirilir. Reset ayrıca numaralandırıcıyı bu konuma geri getirir. Bu konumda tanımlanmamıştır Current . Bu nedenle, değerini Currentokumadan önce numaralandırıcıyı koleksiyonun ilk öğesine ilerletmek için çağrısında MoveNext bulunmalısınız.

Currentveya Reset çağrılana kadar MoveNext aynı nesneyi döndürür. MoveNext bir sonraki öğeye ayarlar Current .

Koleksiyonun sonunu geçerse MoveNext , numaralandırıcı koleksiyondaki son öğeden sonra konumlandırılır ve MoveNext döndürür false. Numaralandırıcı bu konumda olduğunda, sonraki çağrıları MoveNext da döndürür false. döndürülen falseCurrent son çağrı MoveNext tanımlanmamışsa. Koleksiyonun ilk öğesine yeniden ayarlamak Current için öğesini ve ardından MoveNextöğesini çağırabilirsinizReset.

Bir numaralandırıcı, koleksiyon değişmeden kaldığı sürece geçerli kalır. Ekleme, değiştirme veya öğeleri silme gibi, koleksiyona bir değişiklik yapılırsa, numaralandırıcı geri çevrilemez biçimde geçersiz kılınır ve davranışı tanımlanmamış olur.

Numaralandırıcının, koleksiyona özel kullanım erişimi yok; bu nedenle, bir koleksiyon içinde numaralandırma aslında bir iş parçacığı açısından güvenli yordam değildir. Numaralandırma sırasında iş parçacığı güvenliği sağlamak için tüm numaralandırma sırasında koleksiyonu kilitleyebilirsiniz. Okuma ve yazma için birden çok iş parçacığı tarafından erişilecek koleksiyona izin vermek için kendi eşitlemenizi uygulamalısınız.

Bu yöntem bir O(1) işlemdir.

Şunlara uygulanır

Ürün Sürümler
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Ayrıca bkz.