anonymous-methods

C#: Anonymous method vs Named method

社会主义新天地 提交于 2019-11-28 10:09:55
I'm new to SO and programming and learning day by day with bits and pieces of tech (C#) jargons. After Googling for a while, below is what I've researched about methods A Method is a block of statements, which serves for code reusability & it also supports overloading with different SIGNATURE....for ex: drawShape(2pts), drawShape(3pts) etc... An Anonymous method is one with block of statements, but no name....(as its premature to ask, in wt situation we come across anonymous method...any articles, samples ...) Named method : Here's a link but at the end i didn't get what Named Method actually

Can I move Delphi TThread.Synchronize() locally to a VCL form to be called from both a main or worker thread?

為{幸葍}努か 提交于 2019-11-28 09:26:24
问题 I am using CreateAnonymousThread for a worker task, and when I started with it I used Synchronize within the entire declaration as per documented examples, e.g: procedure Txxx.RunWorker; begin FExecutionThread := TThread.CreateAnonymousThread(procedure () begin TThread.Synchronize (TThread.CurrentThread, procedure () begin // Here before worker stuff NotifyBeforeWorkerStuff; end); // Do worker stuff TThread.Synchronize (TThread.CurrentThread, procedure () begin // Here after worker stuff

C# -Closure -Clarification

岁酱吖の 提交于 2019-11-28 09:18:34
I am learning C#.Can I mean closure as a construct that can adopt the changes in the environment in which it is defined. Example : List<Person> gurus = new List<Person>() { new Person{id=1,Name="Jon Skeet"}, new Person{id=2,Name="Marc Gravell"}, new Person{id=3,Name="Lasse"} }; void FindPersonByID(int id) { gurus.FindAll(delegate(Person x) { return x.id == id; }); } The variable id is declared in the scope of FindPersonByID() but t we still can access the local variable id inside the anonymous function (i.e) delegate(Person x) { return x.id == id; } (1) Is my understanding of closure is

C# Cannot use ref or out parameter inside an anonymous method body

有些话、适合烂在心里 提交于 2019-11-28 08:53:09
I'm trying to create a function that can create an Action that increments whatever integer is passed in. However my first attempt is giving me an error "cannot use ref or out parameter inside an anonymous method body". public static class IntEx { public static Action CreateIncrementer(ref int reference) { return () => { reference += 1; }; } } I understand why the compiler doesn't like this, but nonetheless I'd like to have a graceful way to provide a nice incrementer factory that can point to any integer. The only way I'm seeing to do this is something like the following: public static class

Can an anonymous method in C# call itself?

被刻印的时光 ゝ 提交于 2019-11-28 04:50:00
I have the following code: class myClass { private delegate string myDelegate(Object bj); protected void method() { myDelegate build = delegate(Object bj) { var letters= string.Empty; if (someCondition) return build(some_obj); //This line seems to choke the compiler else string.Empty; }; ...... } } Is there another way to set up an anonymous method in C# such that it can call itself? You can break it down into two statements and use the magic of captured variables to achieve the recursion effect: myDelegate build = null; build = delegate(Object bj) { var letters= string.Empty; if

Calling newly defined method from anonymous class

旧时模样 提交于 2019-11-27 22:48:38
I instantiated an object of an anonymous class to which I added a new method. Date date = new Date() { public void someMethod() {} } I am wondering if it is possible to call this method from outside somehow similar to: date.someMethod(); Good question. Answer is No. You cannot directly call date.someMethod(); Let's understand first what is this. Date date = new Date() { ... }; Above is anonymous(have no name) sub-class which is extending Date class. When you see the code like, Runnable r = new Runnable() { public void run() { } }; It means you have defined anonymous(have no name) class which

When not to use lambda expressions [closed]

我与影子孤独终老i 提交于 2019-11-27 17:12:37
A lot of questions are being answered on Stack Overflow, with members specifying how to solve these real world/time problems using lambda expressions . Are we overusing it, and are we considering the performance impact of using lambda expressions? I found a few articles that explores the performance impact of lambda vs anonymous delegates vs for / foreach loops with different results Anonymous Delegates vs Lambda Expressions vs Function Calls Performance Performance of foreach vs. List.ForEach .NET/C# Loop Performance Test (FOR, FOREACH, LINQ, & Lambda) . DataTable.Select is faster than LINQ

Does VB.NET have anonymous functions?

佐手、 提交于 2019-11-27 15:59:58
问题 From what I can find on google, VB.NET only has one-statement lambdas, and not multi-statement anonymous functions. However, all the articles I read were talking about old versions of VB.NET, I couldn't find anything more recent than vs2008 beta 1 or 2. So the question: How can I do this in VB.NET? C# code: private void HandleErrors( Action codeBlock ){ try{ codeBlock(); }catch(Exception e){ //log exception, etc } } HandleErrors(() => { var x = foo(); x.DoStuff(); etc }); 回答1: It does in VB10

Anonymous methods and delegates

纵然是瞬间 提交于 2019-11-27 13:54:41
问题 I try to understand why a BeginInvoke method won't accept an anonymous method. void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { if (InvokeRequired) { //Won't compile BeginInvoke(delegate(object sender, ProgressChangedEventArgs e) { bgWorker_ProgressChanged(sender, e); }); } progressBar1.Increment(e.ProgressPercentage); } It tells me 'cannot convert from 'anonymous method' to 'System.Delegate' while when I cast the anonymous method to a delegate it does work ?

In C#, why can't an anonymous method contain a yield statement?

无人久伴 提交于 2019-11-27 10:35:39
I thought it would be nice to do something like this (with the lambda doing a yield return): public IList<T> Find<T>(Expression<Func<T, bool>> expression) where T : class, new() { IList<T> list = GetList<T>(); var fun = expression.Compile(); var items = () => { foreach (var item in list) if (fun.Invoke(item)) yield return item; // This is not allowed by C# } return items.ToList(); } However, I found out that I can't use yield in anonymous method. I'm wondering why. The yield docs just say it is not allowed. Since it wasn't allowed I just created List and added the items to it. Eric Lippert