Skip to content

Instantly share code, notes, and snippets.

@matolg
Created June 18, 2018 20:45
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 matolg/43ad9b981b828782cd3b1009a6c9aa97 to your computer and use it in GitHub Desktop.
Save matolg/43ad9b981b828782cd3b1009a6c9aa97 to your computer and use it in GitHub Desktop.
C# double dispatch example
internal class Program
{
public interface IVisitor
{
void MethodA(object param);
void MethodA(string param);
}
public class VisitorA : IVisitor
{
public void MethodA(object param)
{
Console.WriteLine("VisitorA object");
}
public void MethodA(string param)
{
Console.WriteLine("VisitorA string");
}
}
public class VisitorB : IVisitor
{
public void MethodA(object param)
{
Console.WriteLine("VisitorB object");
}
public void MethodA(string param)
{
Console.WriteLine("VisitorB string");
}
}
private static void Main()
{
IVisitor visitor = new VisitorB();
object param = "123";
visitor.MethodA(param); // VisitorB object
Console.ReadKey();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment