Created
February 3, 2018 22:37
-
-
Save sebingel/b25ad8ba12d5f8d21884a17b8e7c83a2 to your computer and use it in GitHub Desktop.
Virtual in C#
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
using System; | |
namespace ConsoleApplication1 | |
{ | |
internal class A | |
{ | |
public virtual void Foo() | |
{ | |
Console.WriteLine("Foo A"); | |
} | |
public void Bar() | |
{ | |
Console.WriteLine("Bar A"); | |
} | |
} | |
internal class B : A | |
{ | |
public override void Foo() | |
{ | |
Console.WriteLine("Foo B"); | |
} | |
public void Bar() | |
{ | |
Console.WriteLine("Bar B"); | |
} | |
} | |
internal class C : A | |
{ | |
public void Foo() | |
{ | |
Console.WriteLine("Foo C"); | |
} | |
public new void Bar() | |
{ | |
Console.WriteLine("Bar C"); | |
} | |
} | |
internal class Program | |
{ | |
private static void Main() | |
{ | |
A ab = new B(); | |
A ac = new C(); | |
ab.Foo(); | |
ab.Bar(); | |
ac.Foo(); | |
ac.Bar(); | |
Console.WriteLine("-----------"); | |
B bb = new B(); | |
bb.Foo(); | |
bb.Bar(); | |
Console.WriteLine("-----------"); | |
C cc = new C(); | |
cc.Foo(); | |
cc.Bar(); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment