Skip to content

Instantly share code, notes, and snippets.

@brandonbloom

brandonbloom/dispatch.cs

Last active Apr 19, 2018
Embed
What would you like to do?
using System;
class Base
{
public virtual string Method()
{
return "base method";
}
}
class Derived : Base
{
public override string Method()
{
return "derived method";
}
}
static class Extensions
{
public static string Extension(this Base x)
{
return "base extension";
}
public static string Extension(this Derived x)
{
return "derived extension";
}
}
public class Program
{
public static void Main()
{
Base a = new Base();
Base b = new Derived();
Console.WriteLine(a.Method()); // prints "base method"
Console.WriteLine(b.Method()); // prints "derived method"
Console.WriteLine(a.Extension()); // prints "base extension"
Console.WriteLine(b.Extension()); // prints "base extension" (not "derived extension!!")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment