2

Why is this code compiling:

Func<BeExp, IEnumerable<BeExp>> collectFunc;
if (lhs.Kind == BExpKind.BESum)
{
    collectFunc = CollectSumTerms;
}
else
{
    collectFunc = CollectProdTerms;
}

whereas this is not?

Func<BeExp, IEnumerable<BeExp>> collectFunc = lhs.Kind == BExpKind.BESum ? CollectSumTerms : CollectProdTerms;

Error:

Error CS0173 Type of conditional expression cannot be determined because there is no implicit conversion between 'method group' and 'method group'

Sven Bardos
  • 872
  • 10
  • 27
  • I think it is because when you use the operator ":", the left side (when the clause is true) and right (when it's false) has to have the same type . – Soumen Mukherjee Aug 23 '19 at 11:27

1 Answers1

3

You need to provide an exact signature to the conditional operator for at least one method in the group.

var collectFunc = lhs.Kind == BExpKind.BESum ? (Func<BeExp, IEnumerable<BeExp>>)CollectSumTerms : CollectProdTerms;
EylM
  • 5,967
  • 2
  • 16
  • 28