Why hiding method not work in c# -
this question has answer here:
- overriding vs method hiding [duplicate] 3 answers
consider code:
public class { public void method1() { console.writeline("a.method1"); } public virtual void method2() { console.writeline("a.method2"); } } public class b : { public new void method1() { console.writeline("b.method1"); } public override void method2() { console.writeline("b.method2"); } }
and this:
b b = new b(); b.method1(); b.method2(); console.writeline("*********************"); a = b; a.method1(); a.method2();
this result:
b.method1 b.method2 a.method1 b.method2
my question why when call a.method1() a.method1 instead of getting b.method1.and why method hiding not work.
note line:a = b
my question why when call
a.method1()
a.method1
instead of gettingb.method1
. , why method hiding not work.
because call regular, non-virtual method of class a
. new
modifier not change behavior, suppresses warning:
'...' hides inherited member '...'. use new keyword if hiding intended.
Comments
Post a Comment