Empty.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 ValueTask<int> GetCountAsync(bool onlyIfCheap, CancellationToken cancellationToken) => new ValueTask<int>(0);
  17. public IAsyncPartition<TValue> Skip(int count) => this;
  18. public IAsyncPartition<TValue> Take(int count) => this;
  19. public ValueTask<TValue[]> ToArrayAsync(CancellationToken cancellationToken) => new ValueTask<TValue[]>(
  20. #if NO_ARRAY_EMPTY
  21. EmptyArray<TValue>.Value
  22. #else
  23. Array.Empty<TValue>()
  24. #endif
  25. );
  26. public ValueTask<List<TValue>> ToListAsync(CancellationToken cancellationToken) => new ValueTask<List<TValue>>(new List<TValue>());
  27. public ValueTask<Maybe<TValue>> TryGetElementAsync(int index, CancellationToken cancellationToken) => new ValueTask<Maybe<TValue>>(new Maybe<TValue>());
  28. public ValueTask<Maybe<TValue>> TryGetFirstAsync(CancellationToken cancellationToken) => new ValueTask<Maybe<TValue>>(new Maybe<TValue>());
  29. public ValueTask<Maybe<TValue>> TryGetLastAsync(CancellationToken cancellationToken) => new ValueTask<Maybe<TValue>>(new Maybe<TValue>());
  30. public ValueTask<bool> MoveNextAsync() => new ValueTask<bool>(false);
  31. public IAsyncEnumerator<TValue> GetAsyncEnumerator(CancellationToken cancellationToken)
  32. {
  33. cancellationToken.ThrowIfCancellationRequested(); // NB: [LDM-2018-11-28] Equivalent to async iterator behavior.
  34. return this;
  35. }
  36. public ValueTask DisposeAsync() => default;
  37. }
  38. }
  39. }