IEnumerableExtensions.cs 900 B

1234567891011121314151617181920212223242526272829303132
  1. using System.Collections;
  2. namespace PlcSiemens.Core.Extension
  3. {
  4. public static class IEnumerableExtensions
  5. {
  6. public static IEnumerable<T> Safe<T>(this IEnumerable<T> collection)
  7. {
  8. return collection ?? Enumerable.Empty<T>();
  9. }
  10. public static bool Contains<T>(this IEnumerable<T> collection, Predicate<T> condition)
  11. {
  12. return collection.Any(x => condition(x));
  13. }
  14. public static bool IsEmpty<T>(this IEnumerable<T> collection)
  15. {
  16. if (collection == null)
  17. return true;
  18. var coll = collection as ICollection;
  19. if (coll != null)
  20. return coll.Count == 0;
  21. return !collection.Any();
  22. }
  23. public static bool IsNotEmpty<T>(this IEnumerable<T> collection)
  24. {
  25. return !IsEmpty(collection);
  26. }
  27. }
  28. }