0

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?

Jez
  • 27,951
  • 32
  • 136
  • 233
  • 1
    Maybe this other question can help : https://stackoverflow.com/questions/3570167/why-cant-the-c-sharp-constructor-infer-type – vernou Oct 26 '22 at 13:29

0 Answers0