ienumerator

Why does an IEnumerator have to have at least one yield statement, even if it's unreachable?

给你一囗甜甜゛ 提交于 2019-12-02 00:08:38
问题 Why does this code: public IEnumerator Test() { } Gives you an error: Error CS0161 'Test.GetEnumerator()': not all code paths return a value However this code: public IEnumerator Test() { if(false) yield return 0; } Doesn't? (and works as expected; first MoveNext() returns false) When using IEnumerators as coroutines, sometimes you want to make a coroutine (IEnumerator) that doesn't have an async operations yet (is not yielding anything) but might do that in future. 回答1: From C# specification

Why does an IEnumerator have to have at least one yield statement, even if it's unreachable?

可紊 提交于 2019-12-01 21:56:37
Why does this code: public IEnumerator Test() { } Gives you an error: Error CS0161 'Test.GetEnumerator()': not all code paths return a value However this code: public IEnumerator Test() { if(false) yield return 0; } Doesn't? (and works as expected; first MoveNext() returns false) When using IEnumerators as coroutines, sometimes you want to make a coroutine (IEnumerator) that doesn't have an async operations yet (is not yielding anything) but might do that in future. From C# specification: A block that contains one or more yield statements (§8.14) is called an iterator block. Iterator blocks

Manually increment an enumerator inside foreach loop

随声附和 提交于 2019-12-01 17:31:37
I have a nested while loop inside a foreach loop where I would like to advance the enumerator indefinitately while a certain condition is met. To do this I try casting the enumerator to IEnumerator< T > (which it must be if it is in a foreach loop) then calling MoveNext() on the casted object but it gives me an error saying I cannot convert it. Cannot convert type 'System.DateTime' to System.Collections.Generic.IEnumerator via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion. foreach (DateTime time in times) { while (condition) { //

Manually increment an enumerator inside foreach loop

那年仲夏 提交于 2019-12-01 17:08:51
问题 I have a nested while loop inside a foreach loop where I would like to advance the enumerator indefinitately while a certain condition is met. To do this I try casting the enumerator to IEnumerator< T > (which it must be if it is in a foreach loop) then calling MoveNext() on the casted object but it gives me an error saying I cannot convert it. Cannot convert type 'System.DateTime' to System.Collections.Generic.IEnumerator via a reference conversion, boxing conversion, unboxing conversion,

Implementing a bidirectional enumerator in C#

帅比萌擦擦* 提交于 2019-12-01 06:24:35
Is there a way to use yield blocks to implement an IEnumerator<T> which can go backward ( MoveLast() ) as well as forward? Not directly from the iterator block, no. However, the caller can always buffer the results, for example into a List<T> , or just call Reverse() - but this doesn't always apply. No, the state machine generated by the C# compiler is strictly forward. It doesn't even make sense to go backwards in many cases. Imagine an iterator reading from a network stream - to go backwards, it would have to remember everything that it had ever read, because it couldn't rewind time and ask

Implementing a bidirectional enumerator in C#

泪湿孤枕 提交于 2019-12-01 02:57:05
问题 Is there a way to use yield blocks to implement an IEnumerator<T> which can go backward ( MoveLast() ) as well as forward? 回答1: Not directly from the iterator block, no. However, the caller can always buffer the results, for example into a List<T> , or just call Reverse() - but this doesn't always apply. 回答2: No, the state machine generated by the C# compiler is strictly forward. It doesn't even make sense to go backwards in many cases. Imagine an iterator reading from a network stream - to

Multiple Enumerators for a Single C# Class

时光总嘲笑我的痴心妄想 提交于 2019-11-30 21:44:56
I have created a data structure consisting of intervals. The data structure should naturally have an enumerator that enumerates all intervals, but I would like to expose two different enumerators that enumerate the intervals in different order. One of the enumerators enumerate the intervals really fast, but in somewhat arbitrary order. The other enumerates them in lexicographical order, but a bit slower (depends on the intervals though). Depending on what you try to achieve, one enumerator might be to prefer over the other. Is there a way to allow the user to decide which enumerator should be

C# Class is IEnumerable AND an IEnumerator at the same time. What are the issues with this?

家住魔仙堡 提交于 2019-11-30 20:03:31
I have a class called GenericPermutations that is both enumerable and an enumerator. Its job is to take an ordered list of objects and iterate through each permutation of them in order. Example, an integer implemenation of this class could iterate through the following: GenericPermutations<int> p = new GenericPermutations<int>({ 1, 2, 3 }); p.nextPermutation(); // 123 p.nextPermutation(); // 132 p.nextPermutation(); // 213 // etc. So its enumerable in the sense that it contains a 'list' of things you can enumerate over. It's also an enumerator, because its job involves finding the next

Can we use GetEnumerator() without using IEnumerable interface?

我与影子孤独终老i 提交于 2019-11-30 19:25:35
问题 I have a class called Primes and this class implements GetEnumerator() without implementing IEnumerable interface. public class Primes { private long min; private long max; public Primes() : this(2, 100) { } public IEnumerator GetEnumerator() {...} I don't get it. Am I missing something? 回答1: Firstly, as others have said you can introduce your own methods without implementing interfaces anyway - you can write your own Dispose method without implementing IDisposable etc. For well-known

Is there a “HasNext” method for an IEnumerator?

半腔热情 提交于 2019-11-30 13:59:01
问题 With Java Iterator s, I have used the hasNext method to determine whether an iteration has more elements (without consuming an element) -- thus, hasNext is like a " Peek " method. My question: is there anything like a " hasNext " or " Peek " method with C#'s generic IEnumerator s? 回答1: No, unfortunately there isn't. The IEnumerator<T> interface only exposes the following members: Methods: Dispose MoveNext Reset Properties : Current 回答2: No, but in C# you can repeatedly ask for the current