inheritance - c# - How to get property value from derived class? -
this question has answer here:
- can override property in c#? how? 6 answers
i writing console app in c#.
here code:
interface itest { string str { get; } } public class baseclass : itest { public virtual string str { { return "baseclass"; } } } public class derivedclass : baseclass { public new string str { { return "derivedclass"; } } } class program { static void main(string[] args) { itest itest = new derivedclass(); console.writeline(itest.str); console.readkey(); } }
when executing above code, "baseclass" coming output. instead, how "derivedclass" output?
with declaring property new
in derived class you're creating new member isn't called when invoked through interface. use override
instead.
Comments
Post a Comment