Skip to content

Better generic type inference based on type parameter constraints. #15166

Closed
@orthoxerox

Description

@orthoxerox

Steps to Reproduce:

public static void Foo<T, U>(T value)
    where T: IEnumerable<U>
{
	Console.WriteLine(value.FirstOrDefault().ToString());
}

public static void Main(string[] args)
{
	IEnumerable<int> x = new [] { 1, 2 };
	
	Foo(x);
	
	Console.ReadKey();
}

Expected Behavior:

The program compiles and runs. The type of U is inferred to be int, because of T: IEnumerable<U>.

Actual Behavior:

The type arguments for method 'Scratchpad.Program.Foo<T,U>(T)' cannot be inferred from the usage. Try specifying the type arguments explicitly. (CS0411)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions