diff --git a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Concat.cs b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Concat.cs index b3c9c8a416..dfb150e3d1 100644 --- a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Concat.cs +++ b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Concat.cs @@ -46,15 +46,12 @@ internal override ConcatAsyncIterator Concat(IAsyncEnumerable internal override IAsyncEnumerable? GetAsyncEnumerable(int index) { - switch (index) + return index switch { - case 0: - return _first; - case 1: - return _second; - default: - return null; - } + 0 => _first, + 1 => _second, + _ => null, + }; } } diff --git a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Count.cs b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Count.cs index 1c176804b5..492163af4f 100644 --- a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Count.cs +++ b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Count.cs @@ -16,17 +16,13 @@ public static ValueTask CountAsync(this IAsyncEnumerable if (source == null) throw Error.ArgumentNull(nameof(source)); - switch (source) + return source switch { - case ICollection collection: - return new ValueTask(collection.Count); - case IAsyncIListProvider listProv: - return listProv.GetCountAsync(onlyIfCheap: false, cancellationToken); - case ICollection collection: - return new ValueTask(collection.Count); - } - - return Core(source, cancellationToken); + ICollection collection => new ValueTask(collection.Count), + IAsyncIListProvider listProv => listProv.GetCountAsync(onlyIfCheap: false, cancellationToken), + ICollection collection => new ValueTask(collection.Count), + _ => Core(source, cancellationToken), + }; static async ValueTask Core(IAsyncEnumerable source, CancellationToken cancellationToken) { diff --git a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Select.cs b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Select.cs index 275fa6ae17..6070007721 100644 --- a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Select.cs +++ b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Select.cs @@ -17,15 +17,12 @@ public static IAsyncEnumerable Select(this IAsyncEnum if (selector == null) throw Error.ArgumentNull(nameof(selector)); - switch (source) + return source switch { - case AsyncIterator iterator: - return iterator.Select(selector); - case IList list: - return new SelectIListIterator(list, selector); - } - - return new SelectEnumerableAsyncIterator(source, selector); + AsyncIterator iterator => iterator.Select(selector), + IList list => new SelectIListIterator(list, selector), + _ => new SelectEnumerableAsyncIterator(source, selector), + }; } public static IAsyncEnumerable Select(this IAsyncEnumerable source, Func selector) @@ -60,15 +57,12 @@ internal static IAsyncEnumerable SelectAwaitCore(this if (selector == null) throw Error.ArgumentNull(nameof(selector)); - switch (source) + return source switch { - case AsyncIterator iterator: - return iterator.Select(selector); - case IList list: - return new SelectIListIteratorWithTask(list, selector); - } - - return new SelectEnumerableAsyncIteratorWithTask(source, selector); + AsyncIterator iterator => iterator.Select(selector), + IList list => new SelectIListIteratorWithTask(list, selector), + _ => new SelectEnumerableAsyncIteratorWithTask(source, selector), + }; } #if !NO_DEEP_CANCELLATION @@ -79,15 +73,12 @@ internal static IAsyncEnumerable SelectAwaitWithCancellationCore iterator: - return iterator.Select(selector); - case IList list: - return new SelectIListIteratorWithTaskAndCancellation(list, selector); - } - - return new SelectEnumerableAsyncIteratorWithTaskAndCancellation(source, selector); + AsyncIterator iterator => iterator.Select(selector), + IList list => new SelectIListIteratorWithTaskAndCancellation(list, selector), + _ => new SelectEnumerableAsyncIteratorWithTaskAndCancellation(source, selector), + }; } #endif diff --git a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Single.cs b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Single.cs index 2f0e1cb8de..db5fc5bfba 100644 --- a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Single.cs +++ b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Single.cs @@ -21,13 +21,12 @@ static async ValueTask Core(IAsyncEnumerable source, Cancellat { if (source is IList list) { - switch (list.Count) + return list.Count switch { - case 0: throw Error.NoElements(); - case 1: return list[0]; - } - - throw Error.MoreThanOneElement(); + 0 => throw Error.NoElements(), + 1 => list[0], + _ => throw Error.MoreThanOneElement(), + }; } await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false); diff --git a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SingleOrDefault.cs b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SingleOrDefault.cs index 420746052b..dc95815dd9 100644 --- a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SingleOrDefault.cs +++ b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SingleOrDefault.cs @@ -21,13 +21,12 @@ static async ValueTask Core(IAsyncEnumerable source, Cancellat { if (source is IList list) { - switch (list.Count) + return list.Count switch { - case 0: return default!; - case 1: return list[0]; - } - - throw Error.MoreThanOneElement(); + 0 => default!, + 1 => list[0], + _ => throw Error.MoreThanOneElement(), + }; } await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false)) diff --git a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/ToAsyncEnumerable.cs b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/ToAsyncEnumerable.cs index 2f9aa19b5f..4f76b827ad 100644 --- a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/ToAsyncEnumerable.cs +++ b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/ToAsyncEnumerable.cs @@ -16,15 +16,12 @@ public static IAsyncEnumerable ToAsyncEnumerable(this IEnumera if (source == null) throw Error.ArgumentNull(nameof(source)); - switch (source) + return source switch { - case IList list: - return new AsyncIListEnumerableAdapter(list); - case ICollection collection: - return new AsyncICollectionEnumerableAdapter(collection); - } - - return new AsyncEnumerableAdapter(source); + IList list => new AsyncIListEnumerableAdapter(list), + ICollection collection => new AsyncICollectionEnumerableAdapter(collection), + _ => new AsyncEnumerableAdapter(source), + }; } private sealed class AsyncEnumerableAdapter : AsyncIterator, IAsyncIListProvider diff --git a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Union.cs b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Union.cs index 57e9dd00bc..d8cc53cabf 100644 --- a/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Union.cs +++ b/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Union.cs @@ -219,15 +219,13 @@ public UnionAsyncIterator2(IAsyncEnumerable first, IAsyncEnumerable? GetEnumerable(int index) { Debug.Assert(index >= 0 && index <= 2); - switch (index) + + return index switch { - case 0: - return _first; - case 1: - return _second; - default: - return null; - } + 0 => _first, + 1 => _second, + _ => null, + }; } internal override UnionAsyncIterator Union(IAsyncEnumerable next)