Skip to content

Instantly share code, notes, and snippets.

@cammerman
Created August 2, 2011 17:00
Show Gist options
  • Save cammerman/1120655 to your computer and use it in GitHub Desktop.
Save cammerman/1120655 to your computer and use it in GitHub Desktop.
IEnumerable Null Check
public static class IEnumerableExtensions
{
public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> source)
{
return source ?? Enumerable.Empty<T>();
}
}
foreach (var item in items.EmptyIfNull())
{
//do something
}
@davidalpert
Copy link

I'm surprised that the pseudo-monad of IEnumerable doesn't already implement this Maybe-ish behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment