1. foreach statement
The C # compiler converts the foreach statement to the methods and properties of the IEnumerable interface.
Copy Code code as follows:
foreach (person p in persons)
{
Console.WriteLine (P);
}
The foreach statement resolves to the following code snippet.
• Call the GetEnumerator () method to get an enumeration of the arrays
• In the While loop, as long as MoveNext () returns True, the loop continues
• Accessing elements in an array with the current property
Copy Code code as follows:
IEnumerator enumerator = persons. GetEnumerator ();
while (enumerator. MoveNext ())
{
Person P = (person) Enumerator. Current;
Console.WriteLine (P);
}
2. Yield statement
Two forms of yield statements:
Copy Code code as follows:
Yield return <expression>;
Yield break;
• Returns an element of a collection using a yield return statement
• The method or property that contains the yield statement is an iterator. Iterators must meet the following requirements
A. The return type must be IEnumerable, ienumerable<t>, IEnumerator, or ienumerator<t>.
B. It cannot have any ref or out parameters
The yield return statement cannot be located try-catch fast. The yield return statement can be located in a try block of try-finally
Copy Code code as follows:
Try
{
Error:cannot yield a value in the boday of a try blocks with a catch clause
Yield return "test";
}
Catch
{ }
Try
{
//
Yield return "test again";
}
Finally
{ }
Try
{ }
Finally
{
Error:cannot yield in the body of a finally clause
Yield return "";
}
The yield break statement can be in a try block or a catch block, but not in a finally block
The following example is the code that implements a simple collection with the yield return statement, and iterates through the collection with a foreach statement
Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Namespace ConsoleApplication6
{
Class Program
{
static void Main (string[] args)
{
Hellocollection hellocollection = new Hellocollection ();
foreach (string s in hellocollection)
{
Console.WriteLine (s);
Console.ReadLine ();
}
}
}
public class Hellocollection
{
Public ienumerator<string> GetEnumerator ()
{
The yield return statement returns an element of the collection and moves to the next element; yield break can stop the iteration
Yield return "Hello";
Yield return to "world";
}
}
}
Use the yield return statement to implement a class that iterates through the collection in different ways:
Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Namespace ConsoleApplication8
{
Class Program
{
static void Main (string[] args)
{
Musictitles titles = new Musictitles ();
foreach (string title in titles)
{
Console.WriteLine (title);
}
Console.WriteLine ();
foreach (string title in titles.) Reverse ())
{
Console.WriteLine (title);
}
Console.WriteLine ();
foreach (string title in titles.) Subset (2, 2))
{
Console.WriteLine (title);
Console.ReadLine ();
}
}
}
public class Musictitles
{
String[] names = {"A", "B", "C", "D"};
Public ienumerator<string> GetEnumerator ()
{
for (int i = 0; i < 4; i++)
{
Yield return names[i];
}
}
Public ienumerable<string> Reverse ()
{
for (int i = 3; I >= 0; i--)
{
Yield return names[i];
}
}
Public ienumerable<string> subset (int index, int length)
{
for (int i = index; i < index + length; i++)
{
Yield return names[i];
}
}
}
}
Output: