Skip to content

Instantly share code, notes, and snippets.

@pmsanford
Created July 25, 2012 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmsanford/3175107 to your computer and use it in GitHub Desktop.
Save pmsanford/3175107 to your computer and use it in GitHub Desktop.
Demonstrate the difference between method hiding and overriding.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MethodHiding
{
class Program
{
class A
{
public void Foo()
{
System.Console.WriteLine("A::Foo");
}
public virtual void Bar()
{
System.Console.WriteLine("A::Bar");
}
}
class B : A
{
public new void Foo()
{
System.Console.WriteLine("B::Foo");
}
public override void Bar()
{
System.Console.WriteLine("B::Bar");
}
}
static void Main(string[] args)
{
A a = new A();
a.Foo();
a.Bar();
B b = new B();
b.Foo();
b.Bar();
A ab = new B();
ab.Foo();
ab.Bar();
System.Console.ReadLine();
}
}
}
@pmsanford
Copy link
Author

The output of the above is:
A::Foo
A::Bar
B::Foo
B::Bar
A::Foo
B::Bar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment