c# - How to define an iterator interface type? -


I have written a class which isnumerable & lt; T & gt; implements. I have a method that gives MyClass . If I try to do yield returns from within that method, then the compiler tells me "... can not be an Iterator block because ... there is no Iterator interface type".

So, how can I define my own interface iterator type? Is it supposed to be "abstract" (any method can not be defined)?

What I have to do is write a bunch of sequential methods, so each method should return an example of MyClass . But I need to calculate some sort of code to MyClass instead of using some of the underlying data types, I was hoping that I could simply return the yield everywhere I am


@ Oded:

  class SharpQuery: IEnumerable & lt; HtmlNode & gt; {Public intensity search (string selector) {foreach (var n) in this {// filter result returns node; }}}}}  

No, this is not possible. To see if you have a class zoo that is IEnumerable & lt; Animal & gt; is applicable, but there are many other members in it including a zoo a IEnumerable & lt; Animals & gt; but not necessarily opposite - a sequence of animals is a sequence of animals only, there is no zoo guard, no shop, no entry fee or any other thing that makes the zoo a zoo.

When you use the yield return x then the type of return can not be zoo because you do not have any zoos - you have animals The sequence is.

What can you do instead of it new zoo (foo ()) where foo is a IEnumerable & lt; Animal & gt; and adds a constructor for Zoo which is IEnumerable & lt; Animal & gt; .


Comments