C# equality checking

后端 未结 4 753
花落未央
花落未央 2020-12-08 23:43

What\'s your approach on writing equality checks for the structs and classes you create?

1) Does the \"full\" equ

4条回答
  •  醉话见心
    2020-12-09 00:30

    I rarely do anything special for classes; for most regular objects referential equality works fine.

    I even more rarely write a struct; but since structs represent values it is usually appropriate to provide equality etc. This would usually involve everything; Equals, ==, != and IEquatable (since this avoids boxing in scenarios using EqualityComparer.Default.

    The boilerplate isn't usually too problematic, but IIRC tools like resharper can help here.

    Yes, it is advisable to keep Equals and == in sync, and this needs to be done explicitely.

提交回复
热议问题