Note that there are some explanatory texts on larger screens.

plurals
  1. POA problem with exception handling for IEnumerable<T>, it's lazyness-dependent
    text
    copied!<p>I used to create interfaces with <code>IEnumerable&lt;T&gt;</code> as return type, whenever I want to specify that a particular output is read-only. I like it as it's minimalistic, hides implementation details and decouples a callee from a caller.</p> <p>But recently a colleague of mine argued that <code>IEnumerable&lt;T&gt;</code> should be kept for scenarios which involve lazy evaluation only, as otherwise it's unclear for a caller method, where the exception handling should take it's place -- around a method call or around an iteration. Then for the eager evaluation cases with a read-only output I should use a <code>ReadOnlyCollection</code>.</p> <p>Sounds quite reasonable for me, but what would you recommend? Would you agree with that convention for IEnumerable? Or are there better methods for exception handling around IEnumerable?</p> <p>Just in case if my question is unclear, I made a sample class illustrating the problem. Two methods in here have exactly the same signature, but they require different exception handling:</p> <pre><code>public class EvilEnumerable { IEnumerable&lt;int&gt; Throw() { throw new ArgumentException(); } IEnumerable&lt;int&gt; LazyThrow() { foreach (var item in Throw()) { yield return item; } } public void Run() { try { Throw(); } catch (ArgumentException) { Console.WriteLine("immediate throw"); } try { LazyThrow(); } catch (ArgumentException) { Console.WriteLine("No exception is thrown."); } try { foreach (var item in LazyThrow()) { //do smth } } catch (ArgumentException) { Console.WriteLine("lazy throw"); } } } </code></pre> <p><strong>Update 1.</strong> The question is not limited to ArgumentException only. It's about best practices for making friendly class interfaces, that tell you whether they return lazy evaluated result or not, because this influences the exception-handling approach.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload