private void SimpleLambda()
{
dynamic showMessage = x => MessageBox.Show(x);
showMessage("Hello World!");
}
The Error message is : Can't convert lambda expression to dynamic type , because it is not a delegate type
Any help,
This has nothing to do with MessageBox
- as the error message says, you simply can't convert a lambda expression to dynamic
as the compiler has no idea what delegate type to create an instance of.
You want:
Action<string> action = x => MessageBox.Show(x);
Or even use a method group conversion, although then you have to match the return type:
Func<string, DialogResult> func = MessageBox.Show;
You can then use dynamic
if you want:
dynamic showMessage = action; // Or func
showMessage("Hello World!");
Alternatively, you can specify the lambda expression in an explicit delegate instance expression:
dynamic showMessage = new Action<string>(x => MessageBox.Show(x));
private void SimpleLambda()
{
Action<string> showMessage = x => MessageBox.Show(x);
showMessage("Hello World!");
}
You have to declare the delegate type. Otherwise it won't know what type of lambda expression it's supposed to be—x
could be anything. This should work:
Action<string> showMessage = x => MessageBox.Show(x);
See Action<T>
for clarification on what this delegate type is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With