Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 1
    I'm still confused why for anonymous types == doesn't compare values like .Equals? Commented Aug 25, 2012 at 16:20
  • 12
    Because == does not invoke Equals in C# (never!). It invokes operator == and anonymous types don't have that one. So C# uses reference equality.; Is that good or bad? Who knows because it is a trade-off. After all, I have never felt the need for comparing two anonymous type instances. Never happens for some reason. Commented Aug 25, 2012 at 16:23
  • 1
    I agree it's smart from a convenience perspective (GroupBy() etc), but a bit deceptive from a technical perspective. MSDN states "Anonymous types are class types that derive directly from object...". However object does not implement IStructuralEquatable, so that interface is somehow injected behind the curtains, making things a bit confusing (although useful)... Commented Sep 28, 2015 at 15:57
  • 6
    Having operator == return something different from Equals is just asking for trouble. Commented Oct 7, 2015 at 20:58
  • Sorry for bringing this topic back from the grave, but in C# at least the equality operators (== and !=) should only be doing referential equality on reference types, with the exception that the type has value semantics (such as string). In OP's example they have a Person class, typically a person wouldn't be considered a value type. Commented Nov 23, 2018 at 18:05