When I use a method with a generic parameter to create another object, the generic object isn't selecting the most specific constructor. That sounds confusing, so here's some sample code to demonstrate what I mean...
Can anyone explain why the output of this program is:
guid <-- easy - no problem here
object <-- WHY? This should also be "guid"?!
...and how to make the generic Add<T>
function call the correct constructor of C
?? Here's the code:
void Main()
{
B b = new B();
C c = new C(Guid.Empty);
b.Add<Guid>(Guid.Empty);
}
public class B
{
List<C> cs = new List<C>();
public void Add<T>(T v) { cs.Add(new C(v)); }
}
public class C
{
public C(Guid c) { Console.WriteLine("guid"); }
public C(object c) { Console.WriteLine("object"); }
}