Skip to content

LINQ Contains breaking change in .NET 10 #123533

Closed as not planned
Closed as not planned

Description

@Youssef1313

Description

Run the following snippet on .NET 9 and .NET 10.

using System.Collections.ObjectModel;

var first = new MyEquatableClass?[] { null, null, null };
var second = new ReadOnlyCollection<MyEquatableClass?>(new MyEquatableClass[] { new MyEquatableClass() });

Console.WriteLine(first.Concat(second).Contains(new MyEquatableClass()));


class MyEquatableClass : IEquatable<MyEquatableClass>
{
    public bool Equals(MyEquatableClass? other)
        => other is not null;
}

Reproduction Steps

Run above snippet.

.NET 9 prints true.
.NET 10 prints false.

Expected behavior

Either a clear breaking change notice (if it's there somewhere, sorry I missed it!), or maintaining the compatibility.

Actual behavior

A behavioral breaking change that I cannot find a documentation for in https://learn.microsoft.com/en-us/dotnet/core/compatibility/10#core-net-libraries.

Regression?

Yes, likely from #112684

Known Workarounds

No response

Configuration

No response

Other information

No response

Activity

  1. added
    untriaged
    on Jan 23, 2026
  2. added
    needs-area-label
    on Jan 23, 2026
  3. Youssef1313 commented on Jan 23, 2026

    @Youssef1313
    MemberAuthor

    @stephentoub This is causing issues for at least VSTest.

  4. added and removed
    needs-area-label
    on Jan 23, 2026
  5. dotnet-policy-service commented on Jan 23, 2026

    @dotnet-policy-service
    Contributor

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

  6. tannergooding commented on Jan 23, 2026

    @tannergooding
    Member

    Worth calling out that the .NET 9 behavior is itself inconsistent. Consider (noting we explicitly use IEnumerable<T> for the local to ensure LINQ is resolved instead of MemoryExtensions using ROSpan or similar):

    IEnumerable<MyEquatableClass?> first = new MyEquatableClass?[] { null, null, null };
    IEnumerable<MyEquatableClass?> second = new MyEquatableClass[] { new MyEquatableClass() };
    IEnumerable<MyEquatableClass?> third = new MyEquatableClass?[] { null, null, null, new MyEquatableClass() };
    
    Console.WriteLine(first.Concat(second).Contains(new MyEquatableClass()));
    Console.WriteLine(second.Contains(new MyEquatableClass()));
    Console.WriteLine(third.Contains(new MyEquatableClass()));
    
    class MyEquatableClass : IEquatable<MyEquatableClass>
    {
        public bool Equals(MyEquatableClass? other)
            => other is not null;
    }

    Where we get:

    .NET 9:
      True
      False
      False
    
    .NET 10
      False
      False
      False
    

    But if we change to using collection expressions:

    IEnumerable<MyEquatableClass?> first = [null, null, null];
    IEnumerable<MyEquatableClass?> second = [new MyEquatableClass()];
    IEnumerable<MyEquatableClass?> third = [null, null, null, new MyEquatableClass()];
    
    Console.WriteLine(first.Concat(second).Contains(new MyEquatableClass()));
    Console.WriteLine(second.Contains(new MyEquatableClass()));
    Console.WriteLine(third.Contains(new MyEquatableClass()));
    
    class MyEquatableClass : IEquatable<MyEquatableClass>
    {
        public bool Equals(MyEquatableClass? other)
            => other is not null;
    }

    We instead get:

    .NET 9
      True
      True
      False
    
    .NET 10
      True
      True
      False
    
  7. stephentoub commented on Jan 23, 2026

    @stephentoub
    Member

    The user type is incorrectly implementing equality: it's IEquatable implementation differs from its Equals(object) implentation.

  8. Youssef1313 commented on Jan 23, 2026

    @Youssef1313
    MemberAuthor

    I agree that we should have had proper object Equals implementation. But I think it at least needs to be documented as a breaking change?

    It's fine for me if the original behavior isn't restored.

  9. stephentoub commented on Jan 23, 2026

    @stephentoub
    Member

    But I think it at least needs to be documented as a breaking change?

    Violating core contracts results in undefined behavior. It's not considered a breaking change to switch between object.Equals and IEquatable<T>.Equals, because the contract is that they must have the same behavior.

    I'd expect CA1067 to be issuing a diagnostic on MyEquatableClass.

  10. Youssef1313 commented on Jan 23, 2026

    @Youssef1313
    MemberAuthor

    Thanks. I'll file an issue on vstest repo to look into enabled CA1067 then.

  11. removed
    untriaged
    on Jan 23, 2026
  12. jnyrup commented on Jan 25, 2026

    @jnyrup
    Contributor

    Setting a side for a moment that MyEquatableClass breaks the equals contract.

    Stepping though the example below with a debugger, it looks to me as if for the array case the "genericness" is somehow lost along the way, such that T is no longer MyEquatableClass but object.
    That seems to make EqualityComparer<T>.Default resolve to ObjectEqualityComparer<T> instead of GenericEqualityComparer<T>, despite MyEquatableClass implementing IEquatable<MyEquatableClass>

    Is this expected?

    Image
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    var needle = new MyEquatableClass();
    
    var list = new List<MyEquatableClass> { new MyEquatableClass() };
    Console.WriteLine(Enumerable.Contains(list, needle));
    // True
    
    var array = new[] { new MyEquatableClass() };
    Console.WriteLine(Enumerable.Contains(array, needle));
    // False
    
    class MyEquatableClass : IEquatable<MyEquatableClass>
    {
        public bool Equals(MyEquatableClass? other)
            => other is not null;
    }
  13. tannergooding commented on Jan 25, 2026

    @tannergooding
    Member

    For a reference type it doesn't really matter, there won't (shouldn't) really be any perf or other differences here. At most just a obj is MyEquatableClass other check.

  14. added a commit that references this issue on Jan 28, 2026
  15. locked and limited conversation to collaborators on Feb 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions