How many times in the time of coding you had to write an expression as:
List<T> list = new List<T>();
//...
if (list == null || list.Count == 0)
{
// ...
}
public static class Extensions
{
public static bool IsNullOrDefault<T>(this IEnumerable<T> value)
{
return (value == null || value.Count() == default(int));
}
}
List<string> list = new List<string>();
bool isTrue = list.IsNullOrDefault();
list.Add("abc");
bool isFalse = list.IsNullOrDefault();
int[] numbers = null;
isTrue = numbers.IsNullOrDefault();
numbers = new int[] {1};
isFalse = numbers.IsNullOrDefault();
Enjoy!
No comments:
Post a Comment