0

I have a choice between two statically defined converters, and I want to use a ternary expression to make the choice:

Func<Input, Output> converter = useConverter1 
    ? MyConverter.Converter1
    : MyConverter.Converter2;

The compiler will not accept this expression. It fails with a message saying "There is no implicit conversion between 'method group' and 'method group'".

The structure of MyConverteris

public static class MyConverter
{
    public static Output Converter1(Input input)
    {
        ....
    }

    public static Output Converter1(Input input)
    {
        ....
    }
}

Can I work around this without an if-else statement?

Rob Lyndon
  • 12,089
  • 5
  • 49
  • 74
  • 2
    Your question contains no lambdas. Your conditional operator has two *method groups* as its second and third operands, not lambdas, although if you *did* have lambdas, they'd have the exact same problem for pretty much the same reason. – Servy Mar 07 '18 at 15:10
  • Understood. Thanks. – Rob Lyndon Mar 07 '18 at 15:19
  • 1
    Here's another good [duplicate](https://stackoverflow.com/questions/6308328/type-of-conditional-expression-cannot-be-determined-func) that is more direct about the issue and resolution. – Blake Thingstad Mar 07 '18 at 15:22

0 Answers0