What's going on with this Generic Factory
<p>I’ve been working on a generic factory recently which bypasses the ‘where’ constraint regarding constructors with parameters, and due to a mistake, I came across something which I don’t quite understand and wondered if anyone would be able to shine some light on it.</p>

<p>The lines of code in question are:</p>

<pre><code>ITestInterface myObj = new GenericFactory&lt;ITestInterface&gt;(
    () =&gt; (new TestClass("username", "password"))
).CreateObject() as ITestInterface;

ITestInterface myNewObj = new GenericFactory&lt;ITestInterface&gt;(
    (string x, string y) =&gt; (new TestClass(x, y))
).CreateObject("username ", " password") as ITestInterface;
</code></pre>

<p>Both of these lines do the same thing, but I expected the first one to fail.</p>

<p>The code in the factory is as follows:</p>

<pre><code>public GenericFactory (Func&lt;T&gt; getNewT)
{
   _getNewObject = getNewT;
}

public GenericFactory(Func&lt;string, string, T&gt; getNewT)
{
   _getNewObjectTwoParams = getNewT;
}

public T CreateObject ()
{
   if (_getNewObject == null)
   {
      return default(T);
   }
   else
   {
       return _getNewObject();
   }
}

public T CreateObject (string username, string password)
{
   if (_getNewObjectTwoParams == null)
   {
      return default(T);
   }
   else
   {
      return _getNewObjectTwoParams(username, password);
   }
}
</code></pre>

<p>The TestClass has the following constructor:</p>

<pre><code>public TestClass (string name, string password)
{
   _name = name;
   _password = password;
}
</code></pre>

<p>If anyone could shine some light on why the first call to the factory works I'd be very grateful.</p>

<p>Thanks.</p>
