I have a base class MyBase
and about a dozen of derived classes. I rely on the visitor pattern heavily in my code. So the base class is an abstract host for a visitor, each of the derived classes is a concrete host. I am using a standalone comparer that implements the IEqualityComparer<MyBase>
interface. There are 2 methods in this interface: Boolean Equals(MyBase a, MyBase b)
and Int32 GetHashCode(MyBase obj)
. In each of these methods I use a visitor in order to resolve an instance of MyBase
to the instance of one of the derived types. This is how I avoid dealing with casting. So the visitor is an object that needs to be created on each call to Equals
and GetHashCode
. I've read a lot about keeping GetHashCode
as cheap as possible, so the question would be:
Is creating an object (a visitor) in the GetHashCode
and Equals
method a bad idea considering the performance?