anonymous-methods

Private field captured in anonymous delegate

半世苍凉 提交于 2019-12-03 05:54:56
class A { public event EventHandler AEvent; } class B { private A _foo; private int _bar; public void AttachToAEvent() { _foo.AEvent += delegate() { ... UseBar(_bar); ... } } } Since delegate captures variable this._bar , does it implicitly hold to the instance of B ? Will instance of B be referenced through the event handler and captured variable by an instance of A ? Would it be different if _bar was a local variable of the AttachToAEvent method? Since in my case an instance of A lives far longer and is far smaller than an instance of B , I'm worried to cause a "memory leak" by doing this.

Lambda expression vs anonymous methods [duplicate]

微笑、不失礼 提交于 2019-12-03 05:39:08
问题 This question already has answers here : delegate keyword vs. lambda notation (6 answers) Closed 5 years ago . I would like to know what is the difference. Currently I am learning this stuff and it seems to me like these are just the same: delegate void X(); X instanceOfX; instanceOfX = delegate() { code }; instanceOfX = () => { code }; Also if the lambda are newer, should I just use lambda and forget on anonymous methods? 回答1: Yes, lambda expressions are just very special anonymous methods.

Can someone explain Anonymous methods to me?

巧了我就是萌 提交于 2019-12-03 05:36:15
问题 Delphi 2009, among some cool stuff, has also just got Anonymous methods. I've seen the examples, and the blog posts regarding anonymous methods, but I don't get them yet. Can someone explain why I should be excited? 回答1: Just think of typical callback code where you need to have data available to the callback. Often this data is needed for the callback only , yet you have to jump through a number of hoops to get it there without having to resign to un-OOP-friendly practices like global

Are anonymous listeners incompatible with weak references?

放肆的年华 提交于 2019-12-03 05:08:30
I was reading this question that just got asked: Avoid memory leaks in callbacks? And I was quite confused, until someone answered the following: "The problem with this approach is you cannot have a listener which is only referenced in the collection as it will disappear randomly (on the next GC)" Am I correct in my understanding that using a weak references, like when stored in a WeakHashMap , is incompatible with anonymous listeners? I typically pass listeners like this: public static void main(String[] args) { final Observable obs = new SomeObservable(); obs.addObserver(new Observer() {

Delegates and Lambdas and LINQ, Oh My!

∥☆過路亽.° 提交于 2019-12-02 23:32:25
As a fairly junior developer, I'm running into a problem that highlights my lack of experience and the holes in my knowledge. Please excuse me if the preamble here is too long. I find myself on a project that involves my needing to learn a number of new (to me) technologies, including LINQ (to OBJECTS and to XML for purposes of this project) among others. Everything I've read to this point suggests that to utilize LINQ I'll need to fully understand the following (Delegates, Anonymous Methods and Lambda Expressions). OK, so now comes the fun. I've CONSUMED delegates in the past as I have worked

LINQ vs Lambda vs Anonymous Methods vs Delegates

∥☆過路亽.° 提交于 2019-12-02 21:59:39
Can anyone explain what are the LINQ, Lambda, Anonymous Methods, Delegates meant? How these 3 are different for each other? Was one replaceable for another? I didn't get any concrete answer when i did Googling LINQ is a broad technology name covering a large chunk of .NET 3.5 and the C# 3.0 changes; "query in the language" and tons more. A delegate is comparable to a function-pointer; a "method handle" as an object, if you like, i.e. Func<int,int,int> add = (a,b) => a+b; is a way of writing a delegate that I can then call. Delegates also underpin eventing and other callback approaches.

Dynamically assigning function implementation in Python

感情迁移 提交于 2019-12-02 20:55:48
I want to assign a function implementation dynamically. Let's start with the following: class Doer(object): def __init__(self): self.name = "Bob" def doSomething(self): print "%s got it done" % self.name def doItBetter(self): print "Done better" In other languages we would make doItBetter an anonymous function and assign it to the object. But no support for anonymous functions in Python. Instead, we'll try making a callable class instance, and assign that to the class: class Doer(object): def __init__(self): self.name = "Bob" class DoItBetter(object): def __call__(self): print "%s got it done

Anonymous methods vs. lambda expression [duplicate]

孤人 提交于 2019-12-02 20:47:50
This question already has answers here : What's the difference between anonymous methods (C# 2.0) and lambda expressions (C# 3.0)? [duplicate] (4 answers) Can anyone provide a concise distinction between anonymous method and lambda expressions? Usage of anonymous method: private void DoSomeWork() { if (textBox1.InvokeRequired) { //textBox1.Invoke((Action)(() => textBox1.Text = "test")); textBox1.Invoke((Action)delegate { textBox1.Text = "test"; }); } } Is it only a normal lambda expression being cast to a strongly typed delegate or there is more of it undercover. I'm well aware that a strongly

C# - anonymous functions and event handlers

﹥>﹥吖頭↗ 提交于 2019-12-02 20:28:06
I have the following code: public List<IWFResourceInstance> FindStepsByType(IWFResource res) { List<IWFResourceInstance> retval = new List<IWFResourceInstance>(); this.FoundStep += delegate(object sender, WalkerStepEventArgs e) { if (e.Step.ResourceType == res) retval.Add(e.Step); }; this.Start(); return retval; } Notice how I register my event member (FoundStep) to local in-place anonymous function. My question is: when the function 'FindStepByType' will end - will the anonymous function be removed automatically from the delegate list of the event or I have to manually remove it before

Lambda expression vs anonymous methods [duplicate]

隐身守侯 提交于 2019-12-02 18:59:23
This question already has an answer here: delegate keyword vs. lambda notation 6 answers I would like to know what is the difference. Currently I am learning this stuff and it seems to me like these are just the same: delegate void X(); X instanceOfX; instanceOfX = delegate() { code }; instanceOfX = () => { code }; Also if the lambda are newer, should I just use lambda and forget on anonymous methods? Yes, lambda expressions are just very special anonymous methods. However, there are some deep differences. Start with Eric Lippert's Lambda Expression vs. Anonymous Methods, Part One and continue