Skip to content

[Proposal]: Target-typed inference for constructor calls #9627

@MadsTorgersen

Description

@MadsTorgersen

Target-typed inference for constructor calls

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

Design meetings

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions