c# - How Can I Use IEnumerator.Reset()? -
how right way call ienumerator.reset?
the documentation says:
the
resetmethod provided com interoperability. not need implemented; instead, implementer can thrownotsupportedexception.
okay, mean i'm not supposed ever call it?
it's so tempting use exceptions flow control:
using (enumerator = getsomeexpensiveenumerator()) { while (enumerator.movenext()) { ... } try { enumerator.reset(); } //try inexpensive method catch (notsupportedexception) { enumerator = getsomeexpensiveenumerator(); } //fine, 1 while (enumerator.movenext()) { ... } } is how we're supposed use it? or not meant use managed code @ all?
never; mistake. correct way iterate sequence more once call .getenumerator() again - i.e. use foreach again. if data non-repeatable (or expensive repeat), buffer via .tolist() or similar.
it formal requirement in language spec iterator blocks throw exceptions method. such, cannot rely on working. ever.
Comments
Post a Comment