Copyright statement: This article consistsHttp://leaver.meTranslation. You are welcome to share it with me. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you!
This topic is discussed in this article. Then we will discuss how the ienumerable interface makes the foreach statement usable. Then it will show that if you implement a custom collection class, this collection class implements the ienumerable interface. Yield keywords and traversal sets are also discussed later.
Background
1. Use a set. The traversal set is followed. The best way to traverse a set is to implement the iterator mode-understanding and implementing the iterator pattern in C # And C ++ (this articleArticleC # provides foreach to traverse data in an elegant way.
As long as the set implements the ienumerable interface, you can use foreach to traverse it.
UseCode
First, let's take a look at how the built-in collection class uses foreach to traverse. Arraylist implements the ienumerable interface. Let's take a look.
// let's take a look at how to traverse the set that implements the ienumerable interface arraylist list = New arraylist (); list. add ( " 1 " ); list. add ( 2 ); list. add ( " 3 " ); list. add ( ' 4 ' ); foreach ( Object S in List) {console. writeline (s) ;}
Traverse generic collection classes
Arraylist is a general collection class. You can also traverse generic collection classes. Because these generic collection classes implement the ienumerable <t> interface, let's take a look.
// Traversal implements the generic class of the ienumerable <t> Interface List < String > Listofstrings = New List < String > (); Listofstrings. Add ( " One " ); Listofstrings. Add ( " Two " ); Listofstrings. Add ( " Three " ); Listofstrings. Add ( " Four " ); Foreach ( String S In Listofstrings) {console. writeline (s );}
I found it. The custom collection class or generic collection class should implement the ienumerable and ienumerable <t> interfaces. In this way, you can traverse.
Understanding yield keywords
Before writing an example of an implementation interface, you should first understand the yield keyword. Yield records the position of the set. Yield can be used to return a value from a function.
The following is a common method. No matter how many calls, only one return is returned.
static int simplereturn () { return 1 ; return 2 ; return 3 ;} static void main ( string [] ARGs) { /// let's see console. writeline (simplereturn (); console. writeline (simplereturn (); console. writeline (simplereturn (); console. writeline (simplereturn () ;}
The reason is that normal return statements do not retain the return status of the function. Each call is a new call. Then return the first value.
However, the following statement is different after replacement. When the function is called for the second time. Will continue calling from the last returned location
Static Ienumerable < Int > Yieldreturn (){ Yield Return 1 ; Yield Return 2 ; Yield Return 3 ;} Static Void Main ( String [] ARGs ){ // See the yield return results Foreach ( Int I In Yieldreturn () {console. writeline (I );}}
Obviously, 1, 2, 3 are returned. The only thing to note is that the function must return ienumerable ., Then, call it through foreach.
Implement the ienumerable interface in the custom collection class
Now, if we define a method in our custom set. To iterate all elements. Then return by Using yield. We can achieve this.
Okay. We define the myarraylist class to implement the ienumerable interface, which forces us to implement the getenumerator function. Here we will use yield.
Class Myarraylist: ienumerable { Object [] M_items = Null ; Int Freeindex = 0 ; Public Myarraylist (){ // By the way, I can use arrays directly. Instead, I should use linked lists. M_items = New Object [ 100 ];} Public Void Add ( Object ITEM ){ // When adding elements M_items [freeindex] = Item; freeindex ++ ;} // Ienumerable Function Public Ienumerator getenumerator (){ Foreach ( Object O In M_items ){ // Check whether the end is reached. Array... Not written If (O = Null ){ Break ;} // Returns the current element. Next step forward Yield Return O ;}}}
Then you can use foreach to traverse.
Static Void Main ( String [] ARGs ){ // Look at the call Section Myarraylist mylist = New Myarraylist (); mylist. Add ( " 1 " ); Mylist. Add ( 2 ); Mylist. Add ( " 3 " ); Mylist. Add ( ' 4 ' ); Foreach ( Object S In Mylist) {console. writeline (s );}}
This class. Not written. Not complete. As long as it is for your understanding .. Simulate it.
Implement the ienumerable <t> interface in a custom generic class
Class Mylist <t>: ienumerable <t> {T [] m_items = Null ; Int Freeindex = 0 ; Public Mylist (){ // For convenience. Use Arrays M_items = New T [ 100 ];} Public Void Add (T item ){ // Add Element M_items [freeindex] = Item; freeindex ++ ;} # Region Ienumerable <t> Members Public Ienumerator <t> Getenumerator (){ Foreach (T In M_items ){ // Check whether the end is reached. Array... Not written If (T = Null ) // If t is not an empty type. Just interrupt { Break ;} // Return the current element and proceed Yield Return T ;}} # Endregion # Region Ienumerable members System. Collections. ienumerator system. Collections. ienumerable. getenumerator (){ // The generic version is called here. Return This . Getenumerator ();} # Endregion }
Then you can use foreach.
Static Void Main ( String [] ARGs ){ // Example Mylist < String > Mylistofstrings = New Mylist < String > (); Mylistofstrings. Add ( " One " ); Mylistofstrings. Add ( " Two " ); Mylistofstrings. Add ( " Three " ); Mylistofstrings. Add ( " Four " ); Foreach ( String S In Mylistofstrings) {console. writeline (s );}}
Source codeDownload
Enumerabledemo.7z
Original article address: A-beginners-tutorial-on-implementing-ienumerable-I
Copyright statement: This article by http://leaver.me translation, welcome to repost share. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you!