// statements_foreach_collections.cs
// Using foreach with C#-specific collections:
using System;// Declare the collection:
public class MyCollection 
{
   int[] items;   public MyCollection() 
   {
      items = new int[5] {12, 44, 33, 2, 50};
   }   public MyEnumerator GetEnumerator() 
   {
      return new MyEnumerator(this);
   }   // Declare the enumerator class:
   public class MyEnumerator 
   {
      int nIndex;
      MyCollection collection;
      public MyEnumerator(MyCollection coll) 
      {
         collection = coll;
         nIndex = -1;
      }      public bool MoveNext() 
      {
         nIndex++;
         return(nIndex < collection.items.GetLength(0));
      }      public int Current 
      {
         get 
         {
            return(collection.items[nIndex]);
         }
      }
   }
}public class MainClass 
{
   public static void Main() 
   {
      MyCollection col = new MyCollection();
      Console.WriteLine("Values in the collection are:");      // Display collection items:
      foreach (int i in col) 
      {
         Console.WriteLine(i);
      }
   }
}
输出
Values in the collection are:
12
44
33
2
50

解决方案 »

  1.   

    foreach DataGridItem i in DataGrid.Items
    {
    //
    }
      

  2.   

    string[] a=new string{"a","b","c"};
    foreach(string b in a)
    {
      //循環三次﹐﹐a的值分別為a,b,c
    }
      

  3.   

    其实就是一个for循环
    包装了一下
    用起来更方便
      

  4.   

    foreach比for要复杂。看看chenlm(李逍遥) 的代码。
      

  5.   

    那么vb6.0中的collection 对象如要求达到这样的应用,该怎么操作
      

  6.   

    foreach(DataRow sss in DataRows...)
    {}
    foreach(string xxx in string[]...)
    foreach(int xxx in int[]...)
    foreach(char xxx in char[]...)
    foreach(... in ...S)