-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Open
Description
Target-typed inference for constructor calls
- Specification: https://github.com/dotnet/csharplang/blob/main/proposals/inference-for-constructor-calls.md
- Discussion: [Discussion]: Target-typed inference for constructor calls #9629
Summary
Generic type inference is extended to 'new' expressions, which may infer type arguments for the newly created class or struct, including from a target type if present. For instance, given:
public class MyCollection<T> : IEnumerable<T>
{
public MyCollection() { ... }
...
}We would allow the constructor to be called without a type argument when it can be inferred from arguments or (in this case) a target type:
IEnumerable<string> c = new MyCollection(); // 'T' = 'string' inferred from target type