This has to do with how target-typed new
works. From the proposal, it has the following feature:
A target_typed_new expression does not have a type. However, there is a new object creation conversion that is an implicit conversion from expression, that exists from a target_typed_new to every type.
During overload resolution of MyList
's constructors, it looks at new("John", 30)
and new("Eric", 30)
and finds that there are conversions from those expressions, to both Employee
and IEnumerable<Employee>
, so both overloads are applicable, and neither is better. Hence the call is ambiguous.
In that sense, it's a bit like the null
literal, or the default
literal, and I suspect that l3
doesn't work because of a similar reason that this doesn't work:
var l3 = new MyList<Employee>(
default,
default
);
Though, the documentation also says:
The following are consequences of the specification:
- ...
- The following kinds of types are not permitted as targets of the conversion
- ...
- Interface types: This would work the same as the corresponding creation expression for COM types.
- ...
and IEnumerable<Employee>
is an interface.
Which suggests that the "whether the conversion is valid" is done after overload resolution, which is weird. But the fact that interfaces are invalid targets is only a "consequence" of the spec, and not explicitly specified (the conversion is specified to be valid to all types, but since target-typed new is a kind of object creation expressions, and you can't use interfaces in object creation expressions), so I guess it's technically fine.