CollectionExtensions.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Abp.Collections.Extensions
  4. {
  5. /// <summary>
  6. /// Extension methods for Collections.
  7. /// </summary>
  8. public static class CollectionExtensions
  9. {
  10. /// <summary>
  11. /// Checks whatever given collection object is null or has no item.
  12. /// </summary>
  13. public static bool IsNullOrEmpty<T>(this ICollection<T> source)
  14. {
  15. return source == null || source.Count <= 0;
  16. }
  17. /// <summary>
  18. /// Adds an item to the collection if it's not already in the collection.
  19. /// </summary>
  20. /// <param name="source">Collection</param>
  21. /// <param name="item">Item to check and add</param>
  22. /// <typeparam name="T">Type of the items in the collection</typeparam>
  23. /// <returns>Returns True if added, returns False if not.</returns>
  24. public static bool AddIfNotContains<T>(this ICollection<T> source, T item)
  25. {
  26. if (source == null)
  27. {
  28. throw new ArgumentNullException("source");
  29. }
  30. if (source.Contains(item))
  31. {
  32. return false;
  33. }
  34. source.Add(item);
  35. return true;
  36. }
  37. }
  38. }