Skip to content

Instantly share code, notes, and snippets.

@ormaaj
Last active December 22, 2015 01:29
Show Gist options
  • Select an option

  • Save ormaaj/6396975 to your computer and use it in GitHub Desktop.

Select an option

Save ormaaj/6396975 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
namespace Program {
public abstract class A {
protected abstract string Name { get; set; }
public abstract void Print();
}
class B : A {
protected override string Name { get; set; }
public B() { Name = "B"; }
public override void Print() { Console.Write("{0} ", Name); }
}
class C1 : B {
protected override string Name { get; set; }
public C1() { Name = "C1"; }
public override void Print() { Console.Write("{0} ", Name); }
}
class C2 : B {
new protected string Name { get; set; }
public C2() { Name = "C2"; }
new public void Print() { Console.Write("{0} ", Name); }
}
static class Tests {
public static void Test1() {
var myC1 = new C1();
var myC2 = new C2();
myC1.Print(); ((A)myC1).Print(); myC2.Print(); ((A)myC2).Print();
}
public static void Test2() {
foreach (var o in new List<A> { new C1(), new C2() }) {
o.Print();
((A)o).Print();
}
}
public static void Test3() {
var f = new Action<A> (x => { x.Print(); ((A)x).Print(); });
f(new C1()); f(new C2());
}
}
class Program {
static void Main(string[] argv) {
Tests.Test1(); Console.WriteLine(); // C1 C1 C2 B
Tests.Test2(); Console.WriteLine(); // C1 C1 B B
Tests.Test3(); Console.WriteLine(); // C1 C1 B B
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment