EnumerableExtensions.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Collections.Generic;
  3. namespace MediaBrowser.Common.Extensions
  4. {
  5. /// <summary>
  6. /// Static extensions for the <see cref="IEnumerable{T}"/> interface.
  7. /// </summary>
  8. public static class EnumerableExtensions
  9. {
  10. /// <summary>
  11. /// Determines whether the value is contained in the source collection.
  12. /// </summary>
  13. /// <param name="source">An instance of the <see cref="IEnumerable{String}"/> interface.</param>
  14. /// <param name="value">The value to look for in the collection.</param>
  15. /// <param name="stringComparison">The string comparison.</param>
  16. /// <returns>A value indicating whether the value is contained in the collection.</returns>
  17. /// <exception cref="ArgumentNullException">The source is null.</exception>
  18. public static bool Contains(this IEnumerable<string> source, ReadOnlySpan<char> value, StringComparison stringComparison)
  19. {
  20. if (source == null)
  21. {
  22. throw new ArgumentNullException(nameof(source));
  23. }
  24. if (source is IList<string> list)
  25. {
  26. int len = list.Count;
  27. for (int i = 0; i < len; i++)
  28. {
  29. if (value.Equals(list[i], stringComparison))
  30. {
  31. return true;
  32. }
  33. }
  34. return false;
  35. }
  36. foreach (string element in source)
  37. {
  38. if (value.Equals(element, stringComparison))
  39. {
  40. return true;
  41. }
  42. }
  43. return false;
  44. }
  45. }
  46. }