title: Reflection for Constructors
body: <p>Here:</p>
  
  <p><code>defaultCon</code> binds itself to <code>Dog()</code> Constructor</p>
  
  <p><code>legCon</code> binds itself to <code>Dog(int legs)</code> Constructor</p>
  
  <p>Why do we specify </p>
  
  <p><code>new Type[0]</code> in Line X even though there are no parameters in Default Constructor</p>
  
  <p><code>(new[] { typeof(int) })</code> in Line Y ! Curly braces inside argument/parameter field.Similar problem in Line Z.</p>
  
  <p>I searched on StackOverflow - <a href="https://stackoverflow.com/questions/3255697/using-c-sharp-reflection-to-call-a-constructor" title="this">this</a> answer came close but doesn't answer my question. Here is my code:</p>
  
  <pre><code>namespace Coding
  {
      internal class Dog
      {
          public int NumberOfLegs { get; set; }
          public Dog()
          {
              NumberOfLegs = 4;
          }
  
          public Dog(int legs)
          {
              NumberOfLegs = legs;
          }
      }
  
      public class Class1
      {
          static void Main(string[] args)
          {
              /*line X*/  var defaultCon = typeof(Dog).GetConstructor(new Type[0]); // Get Default Constructor without any params
              /*line Y*/  var legCon = typeof(Dog).GetConstructor(new[] { typeof(int) });//get constructor with param int
  
              var defaultDog = defaultCon.Invoke(null) as Dog;//invoke defaultCon for a Dog Object
              /*Line Z*/  var legDog = legCon.Invoke(new object[] { 5 });//invoke legCon for a Dog object(Sets number of legs as 5) as Dog
  
              Console.Read();
          }
      }
  }
  </code></pre>