Empty.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. public static IAsyncEnumerable<TValue> Empty<TValue>() => EmptyAsyncIterator<TValue>.Instance;
  12. internal sealed class EmptyAsyncIterator<TValue> : IAsyncPartition<TValue>, IAsyncEnumerator<TValue>
  13. {
  14. public static readonly EmptyAsyncIterator<TValue> Instance = new EmptyAsyncIterator<TValue>();
  15. public TValue Current => default;
  16. public Task<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken) => Task.FromResult(0);
  17. public IAsyncPartition<TValue> Skip(int count) => this;
  18. public IAsyncPartition<TValue> Take(int count) => this;
  19. public Task<TValue[]> ToArrayAsync(CancellationToken cancellationToken) => Task.FromResult(
  20. #if NO_ARRAY_EMPTY
  21. EmptyArray<TValue>.Value
  22. #else
  23. Array.Empty<TValue>()
  24. #endif
  25. );
  26. public Task<List<TValue>> ToListAsync(CancellationToken cancellationToken) => Task.FromResult(new List<TValue>());
  27. public Task<Maybe<TValue>> TryGetElementAsync(int index, CancellationToken cancellationToken) => Task.FromResult(new Maybe<TValue>());
  28. public Task<Maybe<TValue>> TryGetFirstAsync(CancellationToken cancellationToken) => Task.FromResult(new Maybe<TValue>());
  29. public Task<Maybe<TValue>> TryGetLastAsync(CancellationToken cancellationToken) => Task.FromResult(new Maybe<TValue>());
  30. public ValueTask<bool> MoveNextAsync() => TaskExt.False;
  31. public IAsyncEnumerator<TValue> GetAsyncEnumerator(CancellationToken cancellationToken) => this;
  32. public ValueTask DisposeAsync() => TaskExt.CompletedTask;
  33. }
  34. }
  35. }