CollectionExtensions.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using JetBrains.Annotations;
  2. namespace VberZero.Tools;
  3. public static class CollectionExtensions
  4. {
  5. public static IList<T> RemoveAll<T>([NotNull] this ICollection<T> source, Func<T, bool> predicate)
  6. {
  7. var items = source.Where(predicate).ToList();
  8. foreach (var item in items)
  9. {
  10. source.Remove(item);
  11. }
  12. return items;
  13. }
  14. public static bool Empty<T>(this ICollection<T> source)
  15. {
  16. if (source != null)
  17. return source.Count <= 0;
  18. return true;
  19. }
  20. public static bool NotEmpty<T>(this ICollection<T> source)
  21. {
  22. return !Empty(source);
  23. }
  24. public static bool IsNullOrEmpty<T>(this ICollection<T> source)
  25. {
  26. if (source != null)
  27. return source.Count <= 0;
  28. return true;
  29. }
  30. public static bool AddIfNotContains<T>(this ICollection<T> source, T item)
  31. {
  32. if (source == null)
  33. throw new ArgumentNullException(nameof(source));
  34. if (source.Contains(item))
  35. return false;
  36. source.Add(item);
  37. return true;
  38. }
  39. }