迭代语句中使用下列关键字: do for foreach in while举个例子:// Declare the collection: public class SampleCollection { public int[] items; public SampleCollection() { items = new int[5] { 5, 4, 7, 9, 3 }; } public System.Collections.IEnumerable BuildCollection() { for (int i = 0; i < items.Length; i++) { yield return items[i]; } } }class MainClass { static void Main() { SampleCollection col = new SampleCollection(); // Display the collection items: System.Console.WriteLine("Values in the collection are:"); foreach (int i in col.BuildCollection()) { System.Console.Write(i + " "); } } } 详细内容请参见:http://msdn.microsoft.com/zh-cn/library/65zzykke.aspx
迭代语句中使用下列关键字:
do
for
foreach
in
while举个例子:// Declare the collection:
public class SampleCollection
{
public int[] items; public SampleCollection()
{
items = new int[5] { 5, 4, 7, 9, 3 };
} public System.Collections.IEnumerable BuildCollection()
{
for (int i = 0; i < items.Length; i++)
{
yield return items[i];
}
}
}class MainClass
{
static void Main()
{
SampleCollection col = new SampleCollection(); // Display the collection items:
System.Console.WriteLine("Values in the collection are:");
foreach (int i in col.BuildCollection())
{
System.Console.Write(i + " ");
}
}
}
详细内容请参见:http://msdn.microsoft.com/zh-cn/library/65zzykke.aspx