CollectionExtensions.cs 1.3 KB

12345678910111213141516171819202122232425262728293031
  1. using System.Collections.Generic;
  2. namespace MediaBrowser.Common.Extensions
  3. {
  4. // The MS CollectionExtensions are only available in netcoreapp
  5. public static class CollectionExtensions
  6. {
  7. public static TValue GetValueOrDefault<TKey, TValue> (this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key)
  8. {
  9. dictionary.TryGetValue(key, out var ret);
  10. return ret;
  11. }
  12. // REVIEW: Inline?
  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 IReadOnlyCollection<T> source, IList<T> destination, int index = 0)
  22. {
  23. foreach (T item in source)
  24. {
  25. destination[index++] = item;
  26. }
  27. }
  28. }
  29. }