Skip to content

fix: OfType now accepts a collection of object? #1617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.
// See the LICENSE file in the project root for more information.

using System;
using System.Linq;
Expand Down Expand Up @@ -40,5 +40,17 @@ public async Task OfType_String()
await HasNextAsync(e, "foo");
await NoNextAsync(e);
}

[Fact]
public async Task OfType_NotNullObject()
{
var xs = new object?[] { 42, null, "foo", null }.ToAsyncEnumerable();
var ys = xs.OfType<object>();

var e = ys.GetAsyncEnumerator();
await HasNextAsync(e, 42);
await HasNextAsync(e, "foo");
await NoNextAsync(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Threading;
Expand All @@ -25,14 +25,14 @@ public static partial class AsyncEnumerable
/// <param name="source">The async-enumerable sequence that contains the elements to be filtered.</param>
/// <returns>An async-enumerable sequence that contains elements from the input sequence of type TResult.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
public static IAsyncEnumerable<TResult> OfType<TResult>(this IAsyncEnumerable<object> source)
public static IAsyncEnumerable<TResult> OfType<TResult>(this IAsyncEnumerable<object?> source)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));

return Core(source);

static async IAsyncEnumerable<TResult> Core(IAsyncEnumerable<object> source, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
static async IAsyncEnumerable<TResult> Core(IAsyncEnumerable<object?> source, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
{
await foreach (var obj in source.WithCancellation(cancellationToken).ConfigureAwait(false))
{
Expand Down