Concatenate.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class EnumerableEx
  11. {
  12. /// <summary>
  13. /// Concatenates the input sequences.
  14. /// </summary>
  15. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  16. /// <param name="sources">Source sequences.</param>
  17. /// <returns>Sequence with the elements of the source sequences concatenated.</returns>
  18. public static IEnumerable<TSource> Concat<TSource>(this IEnumerable<IEnumerable<TSource>> sources)
  19. {
  20. if (sources == null)
  21. throw new ArgumentNullException(nameof(sources));
  22. return sources.Concat_();
  23. }
  24. /// <summary>
  25. /// Concatenates the input sequences.
  26. /// </summary>
  27. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  28. /// <param name="sources">Source sequences.</param>
  29. /// <returns>Sequence with the elements of the source sequences concatenated.</returns>
  30. public static IEnumerable<TSource> Concat<TSource>(params IEnumerable<TSource>[] sources)
  31. {
  32. if (sources == null)
  33. throw new ArgumentNullException(nameof(sources));
  34. return sources.Concat_();
  35. }
  36. private static IEnumerable<TSource> Concat_<TSource>(this IEnumerable<IEnumerable<TSource>> sources)
  37. {
  38. foreach (var source in sources)
  39. foreach (var item in source)
  40. yield return item;
  41. }
  42. }
  43. }