简体   繁体   中英

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

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 method, 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?

是继承或包装第三方课程的选择吗?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM