Skip to content

Conversation

@stephentoub
Copy link
Member

Fixes #119569

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR removes the optimization in OfType<T>().Contains() that delegated to IList.Contains() to fix correctness issues when the source contains items that compare equal but are not of the target type, and to handle multidimensional arrays properly.

  • Removes the IList.Contains() delegation optimization from OfTypeIterator<TResult>.Contains()
  • Adds comprehensive test coverage for multidimensional arrays with OfType()
  • Adds test coverage for the type filtering behavior when objects compare equal but are different types

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/libraries/System.Linq/src/System/Linq/OfType.SpeedOpt.cs Removes IList.Contains optimization and adds detailed comment explaining why direct iteration is necessary
src/libraries/System.Linq/tests/OfTypeTests.cs Adds tests for multidimensional arrays and type filtering behavior with custom equality

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-linq
See info in area-owners.md if you want to be subscribed.

@stephentoub stephentoub merged commit 51368d6 into dotnet:main Sep 11, 2025
89 checks passed
@stephentoub stephentoub deleted the oftypecontains branch September 11, 2025 22:54
@stephentoub
Copy link
Member Author

/backport to release/10.0

@github-actions
Copy link
Contributor

@github-actions github-actions bot locked and limited conversation to collaborators Oct 13, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LINQ Contains() crashes with System.RankException when used together with a multidimensional array

3 participants