IEnumerable Extensions Linq AllOrDefault() Each() I recently decided to try and write my own implementation of Linq, which then lead me on to trying to solve some of the problems we have in our code base at work. Our code is littered with the following (obviously a simplified example):

        var items = new List&lt;string&gt; { "apple", "dog", "chair" };

        if (items != null &amp;&amp; items.Any())
        {
            foreach (var item in items)
            {
                Console.WriteLine(item);
            }
        }


Essentially each time we are iterating over a collection we check to make sure the collection is not null and it actually contains at least one item.

I decided to write the following extension methods to alleviate this mess (the ML suffix on the methods was to differentiate from Linq [mostly for testing]):

        public static class EnumerableExtensions
        {
            public static IEnumerable&lt;T&gt; AllOrDefaultML&lt;T&gt;(this IEnumerable&lt;T&gt; items)
            {
                if (items.IsNullML()) return new List&lt;T&gt;();

                return items;
            }

            public static IEnumerable&lt;T&gt; AllOrDefaultML&lt;T&gt;(this IEnumerable&lt;T&gt; items, Func&lt;T, bool&gt; predicate)
            {
                items = items.AllOrDefaultML();

                if (!items.AnyML()) return new List&lt;T&gt;();

                return items.Where(predicate);
            }

            public static IEnumerable&lt;T&gt; EachML&lt;T&gt;(this IEnumerable&lt;T&gt; items, Action&lt;T&gt; fn)
            {
                var e = items.GetEnumerator();
                while (e.MoveNext())
                {
                    fn(e.Current);
                }
                return items;
            }

            public static bool IsNullML(this object item)
            {
                return item == null;
            }

            public static bool IsNullOrEmptyML&lt;T&gt;(this IEnumerable&lt;T&gt; items)
            {
                return !items.AnyML();
            }

            public static bool AnyML&lt;T&gt;(this IEnumerable&lt;T&gt; items)
            {
                if (items.IsNullML()) return false;

                return items.Count() &gt; 0;
            }

            public static bool AnyML&lt;T&gt;(this IEnumerable&lt;T&gt; items, Func&lt;T, bool&gt; predicate)
            {
                if (items == null) return false;

                items = items.Where(predicate);

                return items.Count() &gt; 0;
            }
        }


Essentially with these methods I can write the above code as the following:

        var items = new List&lt;string&gt; { "apple", "dog", "chair" };

        foreach (var item in items.AllOrDefaultML())
        {
            Console.WriteLine(item);
        }


Or even integrate the Each() method:

        var items = new List&lt;string&gt; { "apple", "dog", "chair" };

        items.AllOrDefaultML().EachML(i =&gt; Console.WriteLine(i));


I'm not sure how I feel about the above, but you get the idea.

I've also added a predicate to AllOrDefaultML() which works if the sequence is null:

        foreach (var item in items.AllOrDefaultML(i =&gt; i.Length == 5))
        {
            Console.WriteLine(item);
        }


Linq has a FirstOrDefault() method, why not have an AllOrDefault() which returns you an empty sequence if it's null and doesn't have any items?

I'm hoping to get some insight in to the question above, and also wondering if anyone else has implemented anything like this before? Has it cleaned up your code much? Is it easier to read? Is it confusing for colleagues (particularly new start) who aren't totally aware of what's happening? Is there anything fundamentally wrong with what I have here?
In those places you use new List&lt;T&gt;(), I would recommend using Enumerable.Empty&lt;T&gt;() in its place. It's a little more idiomatic and, as those empty collections are cached by the runtime, repeated calls don't new up more stuff to be GC'd later.

items.Count() &gt; 0 can be replaced with items.Any(), which is more descriptive of what you're trying to test for, and will likely be cheaper computationally. More specifically,

public static bool AnyML&lt;T&gt;(this IEnumerable&lt;T&gt; items, Func&lt;T, bool&gt; predicate)
{
    if (items == null) return false;

    items = items.Where(predicate);

    return items.Count() &gt; 0;
}


can be replaced with

public static bool AnyML&lt;T&gt;(this IEnumerable&lt;T&gt; items, Func&lt;T, bool&gt; predicate)
{
    if (items == null) return false;

    return items.Any(predicate);
}


** EDITED TO ADD **

public static IEnumerable&lt;T&gt; EachML&lt;T&gt;(this IEnumerable&lt;T&gt; items, Action&lt;T&gt; fn)
{
    var e = items.GetEnumerator();
    while (e.MoveNext())
    {
        fn(e.Current);
    }
    return items;
}


has a small bug - items.GetEnumerator() returns an IEnumerator&lt;T&gt;, which implements IDisposable. This means you should Dispose() of it when you're done using it (see https://stackoverflow.com/a/232616/3312 for good info). Write it like this to do so:

    using (var e = items.GetEnumerator())
    {
        while (e.MoveNext())
        {
            fn(e.Current);
        }
    }
    return items;
}