SafeWhere not working?

I’m using the SafeWhere, but today I got a surprise. It doesn’t seem to do what I’d expect (implement a null check).

Here’s the link:

Couldn’t find it in the documentation. Is it supposed to be used like this:


because it fails if mylist == null.

Tried this:


but no luck. Also:


but Safe() converts the List to IEnumerable, which doesn’t have the Find/Where LINQ? extension methods.

Made my own extension method in the end:

public static List<T> SafeX<T>(this List<T> list)
            if (list != null) return list;
            else return new List<T>();

No idea what the purpose of this API was for as it has no usages in the code-base, but it looks like it was added from when we migrated from google code in 2010.

I’ve added a guard against nulls in this commit, but I’d say it’s irrelevant now that C# has null guards so you should probably use list?.Where(...) instead or if you want an empty collection if null you can use list.Safe().Where(...)