Skip to content

Instantly share code, notes, and snippets.

@NobodysNightmare
Last active December 18, 2015 11:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save NobodysNightmare/5776397 to your computer and use it in GitHub Desktop.
Demonstrates the problem in using the "new" keyword
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