Why hiding method not work in c# -


this question has answer here:

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 getting b.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.

see knowing when use override , new keywords.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -