title: C# Polymorphic constructor
body: <p>I have a base class <code>Item</code> and a child class <code>Ball</code> that inherits from <code>Item</code>.</p>
  
  <pre><code>public class Item
  {
      public string Name { get; set; }
      public uint ID { get; set; }
  
      public Item(ItemData data)
      {
          Name = data.Name;
          ID = data.ID;
      }
  }
  
  public class Ball : Item
  {
      public float Radius { get; set; }
  
      public Ball(BallData data) : base(data)
      {
          CatchRate = data.Radius;
      }
  }
  </code></pre>
  
  <p>They're constructor takes in an <code>ItemData</code> or a <code>BallData</code> object. Just like <code>Ball</code> and <code>Item</code>, <code>BallData</code> inherits from <code>ItemData</code>:</p>
  
  <pre><code>public class ItemData
  {
      public string Name { get; set; }
      public uint ID { get; set; }
  }
  
  public class BallData : ItemData
  {
      public float Radius { get; set; }
  }
  </code></pre>
  
  <p>Now, I have an <code>ItemData</code> object <code>itemDataObj</code>. Note that I don't know whether this is also a <code>BallData</code> object (deserialized from and XML file). What I want to do is have the following code create a <code>Ball</code> object if <code>itemDataObj</code> is also a <code>BallData</code> object and create an <code>Item</code> if <code>itemDataObj</code> is just an <code>ItemData</code> object:</p>
  
  <pre><code>new Item(itemDataObj)
  </code></pre>
  
  <p>Basically, I want my constructors to form a chain of responsibily (polymorphic), where the appropriate one is chosen for the respective type of <code>ItemData</code>.</p>