المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : why you can't store a lambda expression in a 'dynamic variable, or 'var variable ?



C# Programming
04-28-2013, 02:37 PM
// compiler error: "Cannot convert lambda expression to type 'dynamic' because it is not a delegate dynamic dynamicFail = (o1, e1) => { MessageBox.Show("store in 'dynamic"); }; // compiler error: "Cannot assign lambda expression to an implicitly-typed local variable"var varFail = (o2, e2) => { MessageBox.Show("store in 'var"); };However, you can assign a lambda to 'Action, or 'Func:Action myAction = (o3, e3) => { MessageBox.Show("Action works"); };Func myFunc = (o4, e4) => {return "Func works";};In the MSDN docs, there is a comment on 'Func, that:
Quote:
The underlying type of any lambda expression is a Func.And, of course, in the MSDN docs for Lambda Expressions, you are told a lambda is an anonymous function "you can use to create delegates or expression tree types."

I am having trouble forming some kind of useful "mental model" about exactly what a lambda is, and how it can be used (in spite of having Skeet's 2nd. edition of "C# in Depth" at hand).

I know we can define an Event with a lambda: so, a lambda can become an EventHandler:button1.Click += (obj, eva) => { MessageBox.Show("button1 clicked"); };Where this "comes home to roost" for me is when I want to pass a lambda expression as a parameter to a method. You can certainly pass an Action to a method, as a parameter:private void MethodTakesAFunc(Func<span class="code-keyword"dynamic/span, span class="code-keyword"dynamic/span, span class="code-keyword"string/span> theFuncParam){}While you can execute a passed in Func like this:private void MethodTakesAFunc(Func<span class="code-keyword"dynamic/span, span class="code-keyword"dynamic/span, span class="code-keyword"string/span> theFuncParam){ MessageBox.Show(theFuncParam(null, null));}To me this way of getting execution by passing in null parameter stubs just seems absolutely weird: is there a simpler way ?

I see a similar problem with passing a lambda expression as a parameter to a method that then attempts to assign that lambda to an EventHandler.

Right now the only way I know to allow either an EventHandler, MouseEventHandler, or KeyPressHandler, to be passed in to a method, and then validly "wired up" to a Control's Event is something like this:private void BindControlToClickEvent(Control theControl, dynamic theEventParam){ theControl.Click += theEventParam;} // create a new EventHandlerEventHandler myClickEvent = new EventHandler((obj, evn) => { MessageBox.Show("myClickEvent");});// bind the new EventHandler to the Click Event of button2BindControlToClickEvent(button2, myClickEvent);And that seems kind of "kinky."

I hope I have exposed enough of my confusion that diagnosis of my condition can proceed http://www.barakasoft.com/script/Forums/Images/smiley_smile.gif I'd really appreciate any guidance !

thanks, Bill
Humans are amphibians: half spirit, half animal; as spirits they belong to the eternal world; as animals they inhabit time. While their spirit can be directed to an eternal object, their bodies, passions, and imagination are in continual change, for to be in time, means to change. Their nearest approach to constancy is undulation: repeated return to a level from which they repeatedly fall back, a series of troughs and peaks.” C.S. Lewis