Demonstrates the problem in using the "new" keyword
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Unchangable | |
{ | |
public void Foo() | |
{ | |
Console.WriteLine("Foo"); | |
} | |
} | |
class A : Unchangable | |
{ | |
public void Bar() | |
{ | |
Foo(); | |
} | |
} | |
class B : A | |
{ | |
public new void Foo() | |
{ | |
Console.WriteLine("Fuh!"); | |
} | |
} | |
main() | |
{ | |
var a = new A(); | |
var b = new B(); | |
a.Foo(); //Foo | |
b.Foo(); //Fuh! | |
a.Bar(); //Foo | |
b.Bar(); //Foo <---- oops | |
(b as A).Foo(); //Foo <---- oops | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment