class A { public override int GetHashCode() { return 1; } } class B : A { public override int GetHashCode() { return ((object)this).GetHashCode(); } } new B().GetHashCode()
this overflows the stack. How can I call Object.GetHashCode()
from B.GetHashCode()
?
edit: B
now inherits from A
.
base (C# Reference)The base keyword is used to access members of the base class from within a derived class: Call a method on the base class that has been overridden by another method. Specify which base-class constructor should be called when creating instances of the derived class.
A base class, in the context of C#, is a class that is used to create, or derive, other classes. Classes derived from a base class are called child classes, subclasses or derived classes. A base class does not inherit from any other class and is considered parent of a derived class.
(edit - misread question)
If you want to get the original object.GetHashCode()
version; you can't - at least, not unless A
makes it available via something like:
protected int GetBaseHashCode() { return base.GetHashCode();}
(and have B
call GetBaseHashCode()
).
The reason it overflows is that GetHashCode
is (obviously) virtual - it doesn't matter if you cast it to object
; it still starts at the most-derived implementation in the actual object, i.e. B.GetHashCode()
(hence the explosion).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With