Comments on: On the evils of yield http://v1.ripper234.com/p/on-evils-of-yield/ Stuff Ron Gross Finds Interesting Sun, 02 Aug 2015 11:03:35 +0000 hourly 1 https://wordpress.org/?v=4.5.3 By: ripper234 http://v1.ripper234.com/p/on-evils-of-yield/comment-page-1/#comment-4866 Thu, 29 Dec 2011 20:40:51 +0000 http://v1.ripper234.com/?p=1008#comment-4866 Yeah, that’s a good book.

It took me a while to remember what this post was about.
C# as a language tends to be very magical. It’s fun most of the time … until it bites you in the ass.

(See C# vs Java)

]]>
By: M. A. Hanin http://v1.ripper234.com/p/on-evils-of-yield/comment-page-1/#comment-4863 Thu, 29 Dec 2011 16:04:35 +0000 http://v1.ripper234.com/?p=1008#comment-4863 “Odds are it should return a List or Collection” – very true!

One thing I took from “framework design guidelines” (a book by Microsoft), is to be flexible about what you accept and strict about what you return. Therefore, it is a good practice to accept an IEnumerable when taking a collection as an argument, but defining the return type as IEnumerable is bad practice (as opposed to, say, returning an array, or List, or even IList).

An IEnumerable really should be the last choice for a return type IMHO, and we should prefer to work with more explicit interfaces or classes when actually processing the collection – or restrict ourselves to actually enumerating (“for each”).
A trivial example is the Count method of the IEnumerable interface – this is an extension method, which counts the number of elements in the collection by… that’s right – enumerating through the entire collection, making it a O(n) operation. If we work with a List, Collection or Array, and use their respective Count/Length instance methods / properties, then we’re executing an O(1) operation.

]]>