C
C#2y ago
Elio

✅ EqualityComparer

Hello, anyone know of this code work ? --> EqualityComparer<ICollection<myclass>> .Default.Equals(myclassS, x.myclassS) cause i'm wondering if this will trigger the equals override inside myclass to compare each element or if it will do an equal on the collection to look if they have the same hashcode or something like that
6 Replies
MODiX
MODiX2y ago
thinker227#5176
REPL Result: Success
class MyClass
{
public override bool Equals(object? other)
{
Console.WriteLine("equals");
return other is MyClass;
}
}

List<MyClass> a = new() { new() };
List<MyClass> b = new() { new() };
EqualityComparer<ICollection<MyClass>>.Default.Equals(a, b)
class MyClass
{
public override bool Equals(object? other)
{
Console.WriteLine("equals");
return other is MyClass;
}
}

List<MyClass> a = new() { new() };
List<MyClass> b = new() { new() };
EqualityComparer<ICollection<MyClass>>.Default.Equals(a, b)
Result: bool
False
False
Compile: 774.298ms | Execution: 95.486ms | React with ❌ to remove this embed.
Thinker
Thinker2y ago
It calls List<T>.Equals (in this case) EqualityComparer<T> doesn't do anything special for collections
Elio
ElioOP2y ago
So if i want to compare 2 list i should make my own method or can i override the equals methods without creating my own List class ?
Thinker
Thinker2y ago
You can use SequenceEqual from Linq
MODiX
MODiX2y ago
thinker227#5176
REPL Result: Success
new[] { 1, 2, 3 }.SequenceEqual(new[] { 1, 2, 3 })
new[] { 1, 2, 3 }.SequenceEqual(new[] { 1, 2, 3 })
Result: bool
True
True
Compile: 456.640ms | Execution: 48.174ms | React with ❌ to remove this embed.
Elio
ElioOP2y ago
😮 i will try right away thanks !
Want results from more Discord servers?
Add your server