C# complex inheritance -
suppose have code this:
interface { int p { get; } } class : { public virtual int p { { return 0; } } } class b : { public override int p { { return 1; } } } class c : b, { public int p { { return 2; } } } c = new c(); ic = new c();
now question whould c.p , ic.p? know 1 , 2, can explain me why?
in case of a c = new c();
, it's going call first function finds correctly overrides function declared in a
. since c.p
doesn't override it, hides it, virtual tree traversal (polymorphic resolution) won't call c.p
function, it'll instead call lowest 1 in inheritance tree, b.p
.
in case of i ic = new c();
, happily call direct interface implementation of p
, since it's not concerned polymorphic virtual call, in case calls c.p
.
note: it's key here c
has i
directly in inheritance declaration (i.e. looks class c : b, i
instead of class c : b
) behavior. if did not, ic.p
call again refer overridden inherited p
c.p
, , return 1.
you should see warning says following, helps give clue isn't done quite right:
'
c.p
' hides inherited member 'b.p
'. make current member override implementation, addoverride
keyword. otherwise addnew
keyword.
Comments
Post a Comment