IEnumerable<T> in C# -
I am trying to compile the following code, but I am getting errors in VS 2008. Can anyone tell me where I am wrong?
using the system; Using System.Collections.Generic; Using System.Linq; Using System.Text; Namespace dummy {The natural number of the public class • Quality: IEnumerable & lt; Int & gt; {Public INNamator & lt; Int & gt; GetEnumerator () {for (int i = 1; i & lt; = 1000; i ++) yield yields i; } IEnumerator IEnumerable.GetEnumerator () {for (int i = 1; i & lt; = 1000; i ++) yield returns i; }} Class Program {Static Wide Men (String [] ARGs) {foreach (new natural securities (IT) in the console). Limit (I); }}}}
Well, the first compiler error I found is that it complains To do that:
To use the common type 'System.Collections.Generic.IEnumerator', arguments of type '1' are required
This line is on line 16, it's the one: >> >> < At the end of the word / code> the cursor after the IEnumerator on r , and Ctrl + (d) T-key), press Ctrl +, you should suggest that you "use" system.collections; Instructions, do this).
Then it compiles and runs. Do you match what you expect?
Also, note that you should always post the actual error messages, in this way we do not know the wrong tree if there is something else wrong in your code that we are not seeing at first glance.
In addition, you can call any of the other methods by using IEnumerable & lt; T & gt; This very simple implementation of
can be easy, so I simplify the implementation of other methods like this:
IEnumerator IEnumerable.GetEnumerator () {GetEnumerator (Return ); // This object will return a // that is available through reference. "IEnumerator & lt; int & gt; GetEnumerator"}
In this way you only apply the actual counter code once.
And at the end see, it shows better (at least in my opinion) the way to write this whole code.
Comments
Post a Comment