Take.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132
  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.Collections.Generic;
  5. namespace System.Linq
  6. {
  7. public static partial class AsyncEnumerable
  8. {
  9. public static IAsyncEnumerable<TSource> Take<TSource>(this IAsyncEnumerable<TSource> source, int count)
  10. {
  11. if (source == null)
  12. throw Error.ArgumentNull(nameof(source));
  13. if (count <= 0)
  14. {
  15. return Empty<TSource>();
  16. }
  17. else if (source is IAsyncPartition<TSource> partition)
  18. {
  19. return partition.Take(count);
  20. }
  21. else if (source is IList<TSource> list)
  22. {
  23. return new AsyncListPartition<TSource>(list, 0, count - 1);
  24. }
  25. return new AsyncEnumerablePartition<TSource>(source, 0, count - 1);
  26. }
  27. }
  28. }