, IEnumerator IEnumerable . , , , . , , . , foreach.
. , IEnumerator IEnumerable .
// .
Using System;
Using System.Collections;
class MyClass {
char[] chrs = { fAf, fBf, 'C1, 'D' };
// chrs. public IEnumerator GetEnumerator() {
Foreach(char ch in chrs) yield return ch;
}
}
class ItrDemo {
static void Main() {
MyClass me = new MyClassO;
Foreach(char ch in me)
Console.Write (ch + 11 ");
Console.WriteLine();
}
}
.
D
, me. chrs .
. -, , MyClass IEnumerator . . -, GetEnumerator (), .
// chrs. public IEnumerator GetEnumerator() {
Foreach(char ch in chrs) yield return ch;
}
MyClass. , GetEnumerator (), IEnumerable. . foreach, chrs. yield return. , chrs. MyClass foreach Main ().
yield C# . , . .
, . . , . , , . for.
|
|
// ,
// .
Using System;
Using System.Collections;
class MyClass { char ch = fAf;
// // , .
public IEnumerator GetEnumerator() {
for(int i=0; i < 26; i++)
yield return (char) (ch + i);
}
}
class ItrDemo2 {
static void Main() {
MyClass me = new MyClass();
Foreach(char ch in me)
Console.Write(ch + " ");
Console.WriteLine();
}
}
.
ABCDEFGHI JKLMNOPQRSTUVWXYZ
yield.
Yield break;