Great guide on IEquatable vs Equals and records! Really helps to understand when to choose each approach. Quick question—how would you handle custom logic in the Equals method if you need to compare objects with more complex properties like collections? And are there any potential downsides to using records in large projects? Cheers its helpful.........!!
Understanding Equality in C# with IEquatable, Not Equals and ==
SpyrosLeader
posted
2 min read
0 Comments
Spyros
•
Thanks, Ben! Glad you found it helpful!
For collections in Equals, you can use SequenceEqual()
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.sequenceequal?view=net-9.0
for lists or ToHashSet().SetEquals() for unordered sets.
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.setequals?view=net-9.0
If the collection has custom objects, make sure they also override Equals.
Regarding records, a downside in big projects is that they create new copies on modification, which can lead to extra memory use.
Great, thanks! I really appreciate you asking because it helps us all improve our knowledge and brings great value to the community. Discussions like these make learning even better!
Hope that helps! Let me know if you have more questions
For collections in Equals, you can use SequenceEqual()
https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.sequenceequal?view=net-9.0
for lists or ToHashSet().SetEquals() for unordered sets.
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.setequals?view=net-9.0
If the collection has custom objects, make sure they also override Equals.
Regarding records, a downside in big projects is that they create new copies on modification, which can lead to extra memory use.
Great, thanks! I really appreciate you asking because it helps us all improve our knowledge and brings great value to the community. Discussions like these make learning even better!
Hope that helps! Let me know if you have more questions
Please log in to add a comment.
Please log in to comment on this post.
More Posts
chevron_left