fork download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. public static void Main()
  6. {
  7. Zoo zoo = new Zoo();
  8. CountingVisitor v = new CountingVisitor();
  9. zoo.Accept(v);
  10. v.Summary();
  11. }
  12.  
  13. interface AnimalVisitor
  14. {
  15. void Visit(Animal animal);
  16. }
  17.  
  18. interface Animal {}
  19.  
  20. class Zoo
  21. {
  22. Animal[] animals = new Animal[]
  23. {
  24. new Giraffe(),
  25. new Zebra(),
  26. new Lion(),
  27. new Penguin()
  28. };
  29.  
  30. public void Accept(AnimalVisitor visitor)
  31. {
  32. foreach (var a in animals)
  33. visitor.Visit(a);
  34. }
  35. }
  36.  
  37. class Giraffe : Animal {}
  38. class Zebra : Animal {}
  39. class Lion : Animal {}
  40. class Penguin : Animal {}
  41.  
  42. class CountingVisitor : AnimalVisitor
  43. {
  44. int numberOfMammals;
  45. int numberOfBirds;
  46. int numberOfMonochromeAnimals;
  47. public void Summary()
  48. {
  49. Console.WriteLine("Discovered {0} mammals, {1} birds", numberOfMammals, numberOfBirds);
  50. Console.WriteLine("Total {0} monochrome animals", numberOfMonochromeAnimals);
  51. }
  52. public void Visit(Animal animal)
  53. {
  54. VisitImpl((dynamic)animal);
  55. }
  56. void VisitImpl(Giraffe giraffe)
  57. {
  58. numberOfMammals++;
  59. }
  60. void VisitImpl(Zebra zebra)
  61. {
  62. numberOfMammals++;
  63. numberOfMonochromeAnimals++;
  64. }
  65. void VisitImpl(Lion lion)
  66. {
  67. numberOfMammals++;
  68. Console.WriteLine("Lions are NOT scary");
  69. }
  70. void VisitImpl(Penguin penguin)
  71. {
  72. numberOfBirds++;
  73. numberOfMonochromeAnimals++;
  74. }
  75. }
  76. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(54,24): error CS1980: Dynamic keyword requires `System.Runtime.CompilerServices.DynamicAttribute' to be defined. Are you missing System.Core.dll assembly reference?
prog.cs(54,13): error CS0518: The predefined type `Microsoft.CSharp.RuntimeBinder.Binder' is not defined or imported
prog.cs(54,13): error CS0518: The predefined type `System.Runtime.CompilerServices.CallSite' is not defined or imported
prog.cs(54,13): error CS0518: The predefined type `System.Runtime.CompilerServices.CallSite' is not defined or imported
prog.cs(54,13): error CS1969: Dynamic operation cannot be compiled without `Microsoft.CSharp.dll' assembly reference
Compilation failed: 5 error(s), 0 warnings
stdout
Standard output is empty