Skip to content
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

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

Merged
merged 1 commit into from
Dec 3, 2021

Conversation

amis92
Copy link
Contributor

@amis92 amis92 commented Oct 15, 2021

What is the nature of your contribution?

Bugfix

When using OfType<T> on an IAsyncEnumerable<T?> the compiler raised CS8620 warning:

Argument of type 'IAsyncEnumerable<object?>' cannot be used for parameter 'source' of type 'IAsyncEnumerable<object>'
in 'IAsyncEnumerable<object> AsyncEnumerable.OfType<object>(IAsyncEnumerable<object> source)'
due to differences in the nullability of reference types.

The warning appeared in the unit test I've added, before I've changed OfType declaration.

The expected behavior is that the compiler doesn't raise a warning, because we're explicitly asking for our collection to be filtered for not-null objects (null has no type in terms of type checks).

The fix is to change the nullability of type parameter for extension method receiver (this IAsyncEnumerable<object>) to be nullable (object?) instead.

This PR fixes #1525, which was the original report of the above issue.

@dnfadmin
Copy link

dnfadmin commented Oct 15, 2021

CLA assistant check
All CLA requirements met.

@clairernovotny clairernovotny merged commit ab88958 into dotnet:main Dec 3, 2021
@amis92 amis92 deleted the fix/1525-oftype-nullability branch December 4, 2021 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix warning when using nullable reference types with OfType operator
4 participants