Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am writing an algorithm which requires the user to create his own class which inherits from a class defined by me. However, the algorithm requires the user to override the Equals and GetHashCode functions from the C# standard libraries.

Can I force the class inherited from my class to implement the GetHashCode and Equals functions?

public abstract int GetHashCode();

Writing this in my base class is not an option, as my base class inherits GetHashCode from it's parent, where it is implemented already.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
450 views
Welcome To Ask or Share your Answers For Others

1 Answer

This is what you're looking for. Since your class is abstract you can pretty much do this without any problem.

public abstract override int GetHashCode();

This despite of it derived from some other class, this makes your sub class must override this method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...