Kind of a weird issue here - not really a problem, but I'm hoping
someone can explain it...
Suppose I have a delegate variable and want to assign to it
conditionally based on the value of a boolean. For example:
public static int DoSomething() { return 1; }
public static int DoSomethingElse() { return 2; }
public static void Main(string[] args)
{
bool b = GetSomeBoolean();
/* Method #1 - This does not work - gives me a compile-time error:
error CS0173: Type of conditional expression cannot be determined
because there is no implicit conversion between 'method group' and
'method group'
*/
Func<int> myFunc = b ? DoSomething : DoSomethingElse;
// the following three work fine
/* Method #2 */
Func<int> myFunc;
if (b) myFunc = DoSomething; else myFunc = DoSomethingElse;
/* Method #3 */
Func<int> myFunc = b ? new Func<int>(DoSomething) :
DoSomethingElse;
/* Method #4 */
Func<int> myFunc = b ? DoSomething : new Func<int>
(DoSomethingElse);
}
Any ideas on why method #1 doesn't work? Obviously there are at least
three other ways of writing this statement (#2,3,4), but I'm curious
as to why #1 fails at compile time, and even more curious why 2, 3,
and 4 work when 1 doesn't. #3 and #4 are identical to #1, except one
method group is wrapped inside of a constructor for Func, which I
thought was always optional.
No big deal, but it's just bugging me...
Thanks
Joe