fork download
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Polymorphism
  5. {
  6. class Program
  7. {
  8.  
  9. public class Car
  10. {
  11. public string Drive()
  12. {
  13. return "Wrrrr!";
  14. }
  15. }
  16.  
  17. public class Dog
  18. {
  19. public string Talk()
  20. {
  21. return "Woof";
  22. }
  23. }
  24.  
  25. static void Main()
  26. {
  27. var car = new Car();
  28. var dog = new Dog();
  29.  
  30. List<object> list = new List<object>();
  31. list.Add(car);
  32. list.Add(dog);
  33.  
  34. foreach (object o in list)
  35. {
  36. if (o is Car)
  37. Console.WriteLine((o as Car).Drive());
  38. else
  39. Console.WriteLine((o as Dog).Talk());
  40. }
  41.  
  42. }
  43. }
  44. }
Success #stdin #stdout 0.04s 36880KB
stdin
Standard input is empty
stdout
Wrrrr!
Woof