title: Why do we need Upcasting & Downcasting in C#?
body: <p>Consider we are having three classes, Shape which is the base class and two other classes Circle and Text that inherits from the base classe (Shape) </p>
  
  <p>Shape.cs </p>
  
  <pre><code>namespace ObjectOriented
  {
      public class Shape
      {
          public int Height { get; set; }
          public int Width { get; set; }
          public int X { get; set; }
          public int Y { get; set; }
  
          public void Draw()
          {
  
          }
      }
  }
  </code></pre>
  
  <p>Text.cs </p>
  
  <pre><code>using System;
  
  namespace ObjectOriented
  {
      public class Text : Shape
      {
          public string FontType { get; set; }
          public int FontSize { get; set; }
  
          public void Weirdo()
          {
              Console.WriteLine("Weird stuff");
          }
  
      }
  }
  </code></pre>
  
  <p>Circle.cs </p>
  
  <pre><code>namespace ObjectOriented
  {
      public class Circle : Shape
      {
  
      }
  }
  </code></pre>
  
  <p>we all know that Upcasting always succeed and that we create a reference to a base class from a subclass reference</p>
  
  <pre><code>Text txt = new Text();
  Shape shape = txt //upcast
  </code></pre>
  
  <p>and downcasting may throw and <strong>InvalidCastException</strong> for example </p>
  
  <pre><code>Text txt = new Text();
  Shape shape = txt; //upcast
  Circle c = (Circle)shape; //DownCast InvalidCastException
  </code></pre>
  
  <p>what i'm confused about is that what are the <strong>situations/cases</strong> that we might need to up/down cast and object ? </p>