1

I have this code:

Action<A, B> fnUpdate = (someBool) ? (a, b) => a.propOne = b : (a, b) => a.propTwo = d;

Why can the compiler not resolve the types of a and b, just because it is assigned in a ternary operator? it seems quite straight forward at face value.

nawfal
  • 70,104
  • 56
  • 326
  • 368
Daniel Robinson
  • 13,806
  • 18
  • 64
  • 112

2 Answers2

1

The C# compiler tries to create the lambdas independently and cannot unambiguously determine the type. Hence cast it to work as expected....

Action<A, B> fnUpdate = (someBool) 
                       ? (Action<A, B> (a, b) => a.propOne = b 
                       : (Action<A, B> (a, b) => a.propTwo = d);
K D
  • 5,889
  • 1
  • 23
  • 35
0

You need to cast at least one of the two functions:

Action<A, B> fnUpdate = someBool
   ? (Action<A, B>)((a, b) => a.propOne = b)
   : (a, b) => a.propTwo = b;
Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80