title: Write stream to console or file based on user choice
body: <pre><code>abstract class Car
  {
      string Name;
      //some other fields
  
      abstract public void Details(Stream stream);
  }
  
  class Maruti : Car
  {
      int x;
      //some other fields
  
      public override void Details(Stream stream)
      {
           // to do
      }
  }
  
  class Mercedes : Car
  {
      int y;
      //some other fields
      public override void Details(Stream stream)
      {
           // to do
      }
  }
  </code></pre>
  
  <p>I have to write the <strong>Details</strong> method such that if stream is console stream, then the output is displayed on console, and if the stream is file stream, then the output is written in a file.</p>
  
  <p>Following is the way I achieved it :</p>
  
  <pre><code>public override void Details(Stream stream)
  {
      string details = "Name : " + this.Name + Environment.NewLine ;
      if (stream is FileStream)
      {
          TextWriter tmp = Console.Out;
          StreamWriter sw2 = new StreamWriter(stream);
          Console.SetOut(sw2);
          Console.WriteLine(details);
          Console.SetOut(tmp);
          sw2.Close();
      }
      else
      {
          Console.WriteLine(details);
      }
  }
  </code></pre>
  
  <p>I used the above method in both my derived classes with some modifications. Is there any better way to do?</p>