c# - Distinction between iterator and enumerator -
an interview question .net 3.5 job "what difference between iterator , enumerator"?
this core distinction make, linq, etc.
anyway, difference? can't seem find solid definition on net. make no mistake, can find meaning of 2 terms different answers. best answer interview?
imo iterator "iterates" on collection, , enumerator provides functionality iterate, has called.
also, using yield keyword said save state. state? there example of benefit occurring?
iterating means repeating steps, while enumerating means going through values in collection of values. enumerating requires form of iteration.
in way, enumerating special case of iterating step getting value collection.
note "usually" – enumerating may performed recursively, recursion , iteration closely related not care small difference.
you may enumerate values not explicitly store in collection. example, can enumerate natural number, primes, or whatever calculate these values during enumeration , not retrieve them physical collection. understand case enumerating virtual collection values defined logic.
i assume reed copsey got point. in c# there 2 major ways enumerate something.
- implement
enumerable, class implementingienumerator - implement iterator
yieldstatement
the first way harder implement , uses objects enumerating. second way easier implement , uses continuations.
Comments
Post a Comment