Consider the following short program:
public interface IFoo<T> {}
public class Foo : IFoo<string> {}
public static class ExtensionFoo
{
public static TFoo Bar<TFoo, T>(this TFoo foo)
where TFoo : IFoo<T>
{
System.Console.WriteLine($"I never got to know what is {default(T)}");
return foo;
}
}
public class Program
{
public static void Main()
{
// Error, types can not be inferred for some reason
var foo = new Foo().Bar();
// Working, but redundant
// var foo = new Foo().Bar<Foo,string>();
// Error, as expected
// var foo = new Foo().Bar<Foo,int>();
}
}
Why the C# type system does not permit the concise expression: var foo = new Foo().Bar()
?
Is there a deep reason why types can not be resolved in such expressions?
Is it possible in general to avoid types to pop up in such fluent compositions?
This was tested on .Net Core 3.1.