Foreach can iterate some. net types, such as int and string.
However, if you want to iterate your own classes, you must write them yourself.
First, we need to implement several methods of IEnumerator and the GetEnumerator () method of IEnumerable.
Code
Foreach (person p in persons)
{
Console. writeline (p)
}
// Will be parsed
IEnumerator enumerator = persons. GetEnumerator ();
While (enumerator. MoveNext ())
{
Person p = (Person) enumerator. Current;
Console. WriteLine (p );
}
That is to say, the foreach loop will call several methods under IEnumerator
These attributes are: public bool MoveNext (), public void Reset (), and public object Current.
As the name implies
Code
Public class Person
{
Public Person (string fName, string lName)
{
This. firstName = fName;
This. lastName = lName;
}
Public string firstName;
Public string lastName;
}
Public class People: IEnumerable
{
Private Person [] _ people;
Public People (Person [] pArray)
{
// _ People = pArray ?????????
_ People = new Person [pArray. Length];
For (int I = 0; I <pArray. Length; I ++)
{
_ People [I] = pArray [I];
}
}
/// <Summary>
/// Interface implementation. The returned result is a Class Object of PeopleEnum.
/// </Summary>
/// <Returns> </returns>
Public IEnumerator GetEnumerator ()
{
Return new PeopleEnum (_ people );
}
}
Public class PeopleEnum: IEnumerator
{
Public Person [] _ people;
// Enumerators are positioned before the first element
// Until the first MoveNext () call.
Int position =-1;
Public PeopleEnum (Person [] list)
{
_ People = list;
}
Public bool MoveNext ()
{
Position ++;
Return (position <_ people. Length );
}
Public void Reset ()
{
Position =-1;
}
Public object Current
{
Get
{
Try
{
Return _ people [position];
}
Catch (IndexOutOfRangeException)
{
Throw new InvalidOperationException ();
}
}
}
}
Class App
{
Static void Main ()
{
Person [] fig = new Person [3]
{
New Person ("John", "Smith "),
New Person ("Jim", "Johnson "),
New Person ("Sue", "Rabon "),
};
People peopleList = new People (inclulearray );
Foreach (Person p in peopleList)
{
Console. WriteLine (p. firstName + "" + p. lastName );
}
Console. Read ();
}
}
Conclusion: To iterate custom classes, the GetEnumerator () method must be implemented to return IEnumerator
IEnumerator provides the method attributes used by foreach.
C #2.0 with the yield keyword, you do not need to implement the IEnumerator interface every time. The method containing the yield statement must return the IEnumerator or IEnumerable interface code.
Public class person
{
Private string _ Name;
Public string Name {get; set ;}
Public person (string name)
{
Name = name;
}
Public override string ToString ()
{
Return this. Name;
}
}
Class people
{
Private person [] _ person;
Public people (person [] person)
{
_ Person = person;
}
Public IEnumerator GetEnumerator ()
{
For (int I = 0; I <_ person. Length; I ++)
{
Yield return _ person [I];
}
}
Public IEnumerable reverse ()
{
For (int I = _ person. Length-1; I> = 0; I --)
{
Yield return _ person [I];
}
}
}
Class Program
{
Static void Main (string [] args)
{
Person [] p = new person [3]
{
New person ("John "),
New person ("Jim "),
New person ("Sue "),
};
People pp = new people (p );
Foreach (person pe in pp)
{
Console. WriteLine (pe );
}
Foreach (person ppp in pp. reverse ())
{
Console. WriteLine (ppp );
}
Console. Read ();
}
}
I think the yield keyword helps us implement the IEnumerator method.
You can also define the iteration method by yourself. For example, the above Code achieves a reverse loop.
Reference: C # Redbook