Brief Description:A generic extension method that performs the same function as IEnumerable.Any(), but does not throw an error when passed a parameter with a null value.Explanation:Often times I will come across code that performs two steps on a nullable collection to make sure it contains elements. The first step checks to make sure the collection is not null, then the second step looks to see that there are greater than zero elements.For example, to make sure a List<T> meets these qualifications before working through each element, code might look like this:List<int> intList = new List<int>();if (    intList != null &&    intList.Any()    ){    foreach (int intElement in intList)    {        // logic here. . .To reiterate, just using .Any() above would be insufficient for the situation where intList hasn't yet been initialized.Because this is a common occurrence in the code I maintain, I've come up with a generic extension method that performs the same function as the above, in one method:static class CLR{    public static bool Populated<TSource>(this IEnumerable<TSource> source)    {        return source != null && source.Any();    }. . .... which renders the first block a little simpler:if (intList.Populated()){    foreach (int intElement in intList)    {        // logic here. . .Are there suggestions for why this should not be implemented, or why it should be implemented in a different form?
C# enumerable property like .Any that accounts for nulls
c#;generics;iterator;null
With C# 6 you could simply do collection?.Any()but in older versions I use a more generic solution for the same thing because there a more situations where something could be null. This is my helper:static class Helpers{    public static TResult If<TArg, TResult>    (        this TArg arg,        Func<TArg, bool> predicate,        Func<TArg, TResult> trueFunc,        Func<TArg, TResult> falseFunc = null    )    {        return             predicate(arg)             ? trueFunc(arg)             : (falseFunc == null ? default(TResult) : falseFunc(arg));    }    public static TResult IfNotNull<TArg, TResult>(        this TArg arg,         Func<TArg, TResult> trueFunc    ) where TArg : class    {        return arg.If(x => x != null, x => trueFunc(x));    }}Examples:var foo = (List<int>)null;var hasAny1 = foo.If(x => x != null, x => x.Any()); // falsevar hasAny2 = foo.IfNotNull(x => x.Any()); // false