As this post discusses, C#'s type inference can't be used to infer a generic class's types even if those types are passed into the constructor as arguments:
public static class Wrapper<T>
{
public Wrapper(T wrapped)
{
Wrapped = wrapped;
}
public T Wrapped { get; set; }
}
// ...
var wrappedInt = new Wrapper(42); // Can't infer int type
var wrappedInt = new Wrapper<int>(42);
It goes on to describe using a Create
method that can infer the type and create the class for you, but why is it that C# can't infer the type in the above instance? Is there a technical reason or was it just not added to the language spec?