You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
usingSystem.Collections.ObjectModel;varfirst=newMyEquatableClass?[]{null,null,null};varsecond=newReadOnlyCollection<MyEquatableClass?>(newMyEquatableClass[]{newMyEquatableClass()});Console.WriteLine(first.Concat(second).Contains(newMyEquatableClass()));classMyEquatableClass:IEquatable<MyEquatableClass>{publicboolEquals(MyEquatableClass?other)=>otheris 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.
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=newMyEquatableClass?[]{null,null,null};IEnumerable<MyEquatableClass?>second=newMyEquatableClass[]{newMyEquatableClass()};IEnumerable<MyEquatableClass?>third=newMyEquatableClass?[]{null,null,null,newMyEquatableClass()};Console.WriteLine(first.Concat(second).Contains(newMyEquatableClass()));Console.WriteLine(second.Contains(newMyEquatableClass()));Console.WriteLine(third.Contains(newMyEquatableClass()));classMyEquatableClass:IEquatable<MyEquatableClass>{publicboolEquals(MyEquatableClass?other)=>otheris not null;}
IEnumerable<MyEquatableClass?>first=[null,null,null];IEnumerable<MyEquatableClass?>second=[newMyEquatableClass()];IEnumerable<MyEquatableClass?>third=[null,null,null,newMyEquatableClass()];Console.WriteLine(first.Concat(second).Contains(newMyEquatableClass()));Console.WriteLine(second.Contains(newMyEquatableClass()));Console.WriteLine(third.Contains(newMyEquatableClass()));classMyEquatableClass:IEquatable<MyEquatableClass>{publicboolEquals(MyEquatableClass?other)=>otheris not null;}
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.
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?
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;varneedle=newMyEquatableClass();varlist=newList<MyEquatableClass>{newMyEquatableClass()};Console.WriteLine(Enumerable.Contains(list,needle));// Truevararray=new[]{newMyEquatableClass()};Console.WriteLine(Enumerable.Contains(array,needle));// FalseclassMyEquatableClass:IEquatable<MyEquatableClass>{publicboolEquals(MyEquatableClass?other)=>otheris not null;}
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.
Activity
Youssef1313 commentedon Jan 23, 2026
@stephentoub This is causing issues for at least VSTest.
dotnet-policy-service commentedon Jan 23, 2026
Tagging subscribers to this area: @dotnet/area-system-linq
See info in area-owners.md if you want to be subscribed.
tannergooding commentedon Jan 23, 2026
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 ofMemoryExtensionsusingROSpanor similar):Where we get:
But if we change to using collection expressions:
We instead get:
stephentoub commentedon Jan 23, 2026
The user type is incorrectly implementing equality: it's IEquatable implementation differs from its Equals(object) implentation.
Youssef1313 commentedon Jan 23, 2026
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.
Also fix object equality
Also fix object equality
stephentoub commentedon Jan 23, 2026
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.
Youssef1313 commentedon Jan 23, 2026
Thanks. I'll file an issue on vstest repo to look into enabled CA1067 then.
jnyrup commentedon Jan 25, 2026
Setting a side for a moment that
MyEquatableClassbreaks 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
Tis no longerMyEquatableClassbutobject.That seems to make
EqualityComparer<T>.Defaultresolve toObjectEqualityComparer<T>instead ofGenericEqualityComparer<T>, despiteMyEquatableClassimplementingIEquatable<MyEquatableClass>Is this expected?
tannergooding commentedon Jan 25, 2026
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 othercheck.Fix .NET 10 regression for traits (#15370)