title: Decorator pattern and C#
body: <p>I tried to run the following sample program in C# I get the output "You are getting a computer" instead of "You’re getting a computer and a disk and a monitor and a KeyBoard".</p>
  
  <p>Why is this happens C# only not in Java.
  I java the same code I get the appropriate output.</p>
  
  <p>If I debug I found created  object hierarchy is correct but invoking computer.getComputer() always goes to Super class not in drived class, that is the problem.</p>
  
  <p>Please help me to solve this issue.</p>
  
  <pre><code>namespace DecoratorTest1
  {
  
      public class Computer
      {
          public Computer()
          {
          }
  
          public  String getComputer()
          {
              return "computer";
          }
      }
  
  
      public abstract class ComponentDecorator : Computer
      {
          public abstract String getComputer();
      }
  
      public class Disk : ComponentDecorator
      {
          Computer computer;
          public Disk(Computer c)
          {
              computer = c;
          }
  
  
          public override String getComputer()
          {
              return computer.getComputer() + " and a disk";
          }
  
      }
  
      public class Monitor : ComponentDecorator
      {
          Computer computer;
          public Monitor(Computer c)
         {
              computer = c;
          }
          public override String getComputer()
          {
              return computer.getComputer() + " and a Monitor";
          }
  
      }
  
      public class KeyBoard : ComponentDecorator
      {
          Computer computer;
          public KeyBoard(Computer c)
         {
              computer = c;
          }
  
          public  override String getComputer()
          {
              return computer.getComputer() + " and a KeyBoard";
          }
  
          public string call()
          {
              return "";
          }
  
      }
  
  
  
      class Program
      {
          static void Main(string[] args)
          {
              Computer computer = new Computer();
              computer = new Disk(computer);
              computer = new Monitor(computer);
              computer = new KeyBoard(computer);
  
  
              Console.WriteLine(" You are getting a " + computer.getComputer());
              Console.ReadKey();
  
          }
      }
  }
  </code></pre>