Here we are talking about filtering list items again :). I got two comment suggesting the use of yield return.
James Curran said:
Don’t build a new list if you don’t have to.
private static IEnumerable EvenSlicker_FilterPositiveItems(IEnumerable t)
{foreach(int n in t)
if (n > 0)
yield return n;
}
The advantage is if you try using the method this this:
foreach(int n in FilterPositiveItems(myList) {…}
Your way is O(2N) while mine is O(N)
We use collections all the time. Many times we have to expose them to users of our classes. Lets look at this simple tree node class:
class TreeNode
{
private List<TreeNode> children = new List<TreeNode>();
public IList<TreeNode> Children
{
[...]
We pay for user submitted tutorials and articles that we publish. Anyone can send in a contribution
Learn More