I was writing some logging code, and I had to invoke either of two methods with the same signature:
void Error(string, Exception)
void Warn(string, Exception)
I decided to approach this with functional programming instead of an if/else. I have a bool wasHandled
to determine which method to invoke. I tried:
Action<string, Exception> logAction = wasHandled ? _logger.Warn : _logger.Error;
But the compiler / intellisense complained with:
Cannot choose method from method group. Did you intend to invoke the method?
Instead, I had to write it like this:
var logAction = wasHandled ? new Action<string, Exception>(_logger.Warn) : _logger.Error;
I get the bonus of var
but I really hate that I have to explicitly invoke new Action....
.
Why does the former not work?