为什么构造函数不像泛型方法那样支持类型推理?
public class MyType<T>
{
private readonly T field;
public MyType(T value) { field = value; }
}
var obj = new MyType(42); // why can't type inference work out that I want a MyType<int>?
虽然你可以用工厂课程来解决这个问题,
public class MyTypeFactory
{
public static MyType<T> Create<T>(T value)
{
return new MyType<T>(value);
}
}
var myObj = MyTypeFactory.Create(42);
构造函数不支持类型推理是否有实际或哲学上的原因?