Empty.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. private sealed class EmptyAsyncIterator<TValue> : IAsyncPartition<TValue>, IAsyncEnumerator<TValue>
  13. {
  14. public static readonly EmptyAsyncIterator<TValue> Instance = new EmptyAsyncIterator<TValue>();
  15. public TValue Current => default(TValue);
  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(Array.Empty<TValue>());
  20. public Task<List<TValue>> ToListAsync(CancellationToken cancellationToken) => Task.FromResult(new List<TValue>());
  21. public Task<Maybe<TValue>> TryGetElementAsync(int index) => Task.FromResult(new Maybe<TValue>());
  22. public Task<Maybe<TValue>> TryGetFirstAsync() => Task.FromResult(new Maybe<TValue>());
  23. public Task<Maybe<TValue>> TryGetLastAsync() => Task.FromResult(new Maybe<TValue>());
  24. public Task<bool> MoveNextAsync() => TaskExt.False;
  25. public IAsyncEnumerator<TValue> GetAsyncEnumerator() => this;
  26. public Task DisposeAsync() => TaskExt.CompletedTask;
  27. }
  28. }
  29. }