开发者

Where to put the equality function for a class in a third party library?

开发者 https://www.devze.com 2023-02-12 01:44 出处:网络
I\'m using a third party library containing a class which lacks both operator==() and Equals(). I\'d like to implement one myself, but I\'m not sure how to name it and where to put it. I\'ve tried to

I'm using a third party library containing a class which lacks both operator==() and Equals(). I'd like to implement one myself, but I'm not sure how to name it and where to put it. I've tried to add both operators as an extension metho开发者_StackOverflow社区d, but both failed. I've now written an IsEqualTo() function, but it results in rather confusing client code. Does anyone know a more elegant solution?


Would it be an option to inherit from or wrap the third party class?


You won't be able to overload the == operator, but you could create an IEqualityComparer<T> which can be used for dictionaries etc.

While you can write an Equals extension method, it would never be used because the virtual method on Object will always be applicable, and if there's an applicable instance method the compiler doesn't look for extension methods even if there's a more specific one.

You might want to rename your IsEqualTo method as ValueEquals or something similar... but I agree it's still not ideal.

0

精彩评论

暂无评论...
验证码 换一张
取 消