Created
August 2, 2011 16:50
-
-
Save ferventcoder/1120638 to your computer and use it in GitHub Desktop.
IEnumerable Null Safeguard Extension - Useful When Iterating (ForEach Loop)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foreach (var item in items ?? new List<string>()) | |
{ | |
//do something | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class EnumerableExtensions | |
{ | |
public static IEnumerable<T> OrEmptyListIfNull<T>(this IEnumerable<T> source) | |
{ | |
return source ?? Enumerable.Empty<T>(); | |
} | |
} | |
foreach(var item in items.OrEmptyListIfNull()) | |
{ | |
//do something | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This feels like community design... :D