1

Why we cannot write something like that:

Func<int, int> operation = i == 1 
    ? (int a, int b) => a + b 
    : (int a, int b) => a * b;

Type of conditional expression cannot be determined because there is no implicit conversion between 'lambda expression' and 'lambda expression'

Tezzy
  • 51
  • 6
  • 5
    I'm assuming you mean to have a `Func`. You can get this to compile if you explicitly cast one of the lambda expressions. I assume this has something to do with not being able to implicitly type a lambda, and the ternary checking that both paths share the same type before checking if they are valid to assign to `operation`. – Jonathon Chase Dec 02 '19 at 18:38
  • Thanks! I managed to do it, but it's ugly `Func operation = i == 1 ?(Func)((int a, int b) => a + b):(int a, int b) => a * b;` – Tezzy Dec 02 '19 at 18:43
  • 1
    You are missing many of the int generics you should in fact have this: Func operation = (op, a, b) => (op == 1) ? a + b : a * b; – N_tro_P Dec 02 '19 at 18:50

0 Answers0