I am writing code similar to this:
public IEnumerable<T> Unfold<T>(this T seed) { while (true) { yield return [next (T)object in custom sequence]; } }
Obviously, this method will never return. (The C # compiler silently resolves this, while R # gives me the warning "The function never returns.")
Generally speaking, does poor design provide an enumerator that returns an infinite number of elements without providing a way to stop the enumeration?
Are there any special considerations for this scenario? Mem? Perf? Other bugs?
If we always provide an exit condition, what are the parameters? For example:
- an object of type T that represents an inclusive or exclusive border
- a
Predicate<T> continue (as TakeWhile ) - account (like
Take ) - ...
Should we rely on users calling Take(...) / TakeWhile(...) after Unfold(...) ? (Perhaps the preferred option, as it uses existing Linq knowledge.)
Could you answer this question differently if the code published a public API, either as it is (general) or as a specific implementation of this template?
c # enumerator
Christoffer lette
source share