fork download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. // 2 наследника, в которых переопределяется невиртуальный метод
  6. class Base { public void DoIt() { Console.WriteLine("Base"); } }
  7. class A : Base { public new void DoIt() { Console.WriteLine("A"); } }
  8. class B : Base { public new void DoIt() { Console.WriteLine("B"); } }
  9.  
  10. static void GenericTest<Smth>(Smth x) where Smth : Base
  11. {
  12. x.DoIt();
  13. }
  14.  
  15. static void DynamicTest(dynamic x)
  16. {
  17. x.DoIt();
  18. }
  19.  
  20. public static void Main()
  21. {
  22. Console.WriteLine("GenericTest");
  23. GenericTest(new Base());
  24. GenericTest(new A());
  25. GenericTest(new B());
  26. Console.WriteLine();
  27.  
  28. Console.WriteLine("DynamicTest");
  29. DynamicTest(new Base());
  30. DynamicTest(new A());
  31. DynamicTest(new B());
  32. Console.WriteLine();
  33. }
  34. }
Success #stdin #stdout 0.4s 31032KB
stdin
Standard input is empty
stdout
GenericTest
Base
Base
Base

DynamicTest
Base
A
B