Type argument inference failing with unambiguous method group #5963
-
C# 10 introduced natural types for lambdas and method groups. However, compilation error CS0411 still occurs when an unambiguous method group is passed as an argument to a generic method. Please consider this case, as discussed in #129 static bool IsEven(int x) => x % 2 == 0;
static void Test<T>(Func<T, bool> predicate)
{
}
// type inference succeeds here
var isEven = IsEven;
// But fails here, why?
Test(IsEven); If the Originally posted by @MorenoGentili in #129 (reply in thread) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Changing Part of the problem might be that this was actually implemented as a "special" conversion which occurs when there's no target type or the target type is |
Beta Was this translation helpful? Give feedback.
-
The local However, the method group (By contrast, type inference of the corresponding lambda expression |
Beta Was this translation helpful? Give feedback.
The local
isEven
has a delegate_type, and type inference forTest(isEven)
will infer from the type arguments of the local typeFunc<int, bool>
to the type parameters of the target typeFunc<T, bool>
: see spec.However, the method group
IsEven
has a function_type rather than a delegate_type, and there is no type inference rule for inferring from the parameter types of a function_type to the type parameters of the target type.(By contrast, type inference of the corresponding lambda expression
Test((int x) => x % 2 == 0);
succeeds not because of function_type of the lambda but because type inference has a specific rule for anonymous functions: see spec.)