CollectionExtensions.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #pragma warning disable CS1591
  2. using System.Collections.Generic;
  3. namespace MediaBrowser.Common.Extensions
  4. {
  5. // The MS CollectionExtensions are only available in netcoreapp
  6. public static class CollectionExtensions
  7. {
  8. public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key)
  9. {
  10. dictionary.TryGetValue(key, out var ret);
  11. return ret;
  12. }
  13. /// <summary>
  14. /// Copies all the elements of the current collection to the specified list
  15. /// starting at the specified destination array index. The index is specified as a 32-bit integer.
  16. /// </summary>
  17. /// <param name="source">The current collection that is the source of the elements.</param>
  18. /// <param name="destination">The list that is the destination of the elements copied from the current collection.</param>
  19. /// <param name="index">A 32-bit integer that represents the index in <c>destination</c> at which copying begins.</param>
  20. /// <typeparam name="T"></typeparam>
  21. public static void CopyTo<T>(this IReadOnlyList<T> source, IList<T> destination, int index = 0)
  22. {
  23. for (int i = 0; i < source.Count; i++)
  24. {
  25. destination[index + i] = source[i];
  26. }
  27. }
  28. /// <summary>
  29. /// Copies all the elements of the current collection to the specified list
  30. /// starting at the specified destination array index. The index is specified as a 32-bit integer.
  31. /// </summary>
  32. /// <param name="source">The current collection that is the source of the elements.</param>
  33. /// <param name="destination">The list that is the destination of the elements copied from the current collection.</param>
  34. /// <param name="index">A 32-bit integer that represents the index in <c>destination</c> at which copying begins.</param>
  35. /// <typeparam name="T"></typeparam>
  36. public static void CopyTo<T>(this IReadOnlyCollection<T> source, IList<T> destination, int index = 0)
  37. {
  38. foreach (T item in source)
  39. {
  40. destination[index++] = item;
  41. }
  42. }
  43. }
  44. }