I assume this question is duplicated. But I could not find this question on SO
I want to instantiate a generic class. But if there is a constructor with a explicit parameter And the generic constructor also has that parameter due to the given type, the constructor with the explicit parameter is used.
Example
class Program
{
static void Main(string[] args)
{
Example<string> test = new Example<string>("test");
test.Print();//Prints test2
}
}
class Example<T>
{
private object Value;
public Example(T value1)
{
this.Value = value1 + "1";
}
public Example(string value2)
{
this.Value = value2 + "2";
}
public void Print()
{
Console.WriteLine(Value as string);
}
}
Is there a way to call the generic constructor?