2

I am wondering why I cannot use ternary operator with Func, Action and Predicate.

Here is a simple example that does not compile:

bool condition = true;
Action show = condition ? () => Console.Write("true") : () => Console.Write("false");

It says:

There is no implicit conversion between 'lambda expression' and 'lambda expression'

Also I cannot do an explicit cast:

Action show = (Action)(true ? () => Console.Write("true") : () => Console.Write("false"));
Andrei
  • 42,814
  • 35
  • 154
  • 218
  • try casting the lambdas to action? Like this (Action)(() => Console.Write("Bleh")) – kevinrodriguez-io Oct 15 '15 at 17:16
  • (I think I've found reasonable duplicate, please comment if that duplicate is not enough... Make sure to search for something like "C# lambda ternary implicit conversion" to see if there is better duplicate as this question is asked a lot). – Alexei Levenkov Oct 15 '15 at 17:30
  • @AlexeiLevenkov technically my question is wider. Practically they are similar. – Andrei Oct 15 '15 at 17:32
  • Try with this code Action show = condition ? new Action(()=> Console.Write("true")) : new Action(() => Console.Write("false")); –  Oct 15 '15 at 17:36

0 Answers0