anonymous-methods

How do you use Func<> and Action<> when designing applications?

≯℡__Kan透↙ 提交于 2019-11-29 18:45:47
All the examples I can find about Func<> and Action<> are simple as in the one below where you see how they technically work but I would like to see them used in examples where they solve problems that previously could not be solved or could be solved only in a more complex way, i.e. I know how they work and I can see they are terse and powerful , so I want to understand them in a larger sense of what kinds of problems they solve and how I could use them in the design of applications. In what ways (patterns) do you use Func<> and Action<> to solve real problems? using System; using System

Scope of anonymous methods

笑着哭i 提交于 2019-11-29 08:25:04
问题 One nice thing about anonymous methods is that I can use variables that are local in the calling context. Is there any reason why this does not work for out-parameters and function results? function ReturnTwoStrings (out Str1 : String) : String; begin ExecuteProcedure (procedure begin Str1 := 'First String'; Result := 'Second String'; end); end; Very artificial example of course, but I ran into some situations where this would have been useful. When I try to compile this, the compiler

Anonymous method as parameter to BeginInvoke?

我的未来我决定 提交于 2019-11-29 06:08:58
问题 Why can't you pass an anonymous method as a parameter to the BeginInvoke method? I have the following code: private delegate void CfgMnMnuDlg(DIServer svr); private void ConfigureMainMenu(DIServer server,) { MenuStrip mnMnu = PresenterView.MainMenu; if (mnMnu.InvokeRequired) { mnMnu.BeginInvoke((CfgMnMnuDlg)ConfigureMainMenu, new object[] { server}); } else { // Do actual work here } } I'm trying to avoid declaring the delegate. Why can't I write something like the below instead? Or can I,

Compiler generated incorrect code for anonymous methods [MS BUG FIXED]

微笑、不失礼 提交于 2019-11-29 01:42:12
问题 See the following code: public abstract class Base { public virtual void Foo<T>() where T : class { Console.WriteLine("base"); } } public class Derived : Base { public override void Foo<T>() { Console.WriteLine("derived"); } public void Bang() { Action bang = new Action(delegate { base.Foo<string>(); }); bang(); //VerificationException is thrown } } new Derived().Bang(); throws an exception. Inside the generated CIL of the method Bang I got: call instance void ConsoleApp.Derived::'<>n_

Closures in C# event handler delegates? [duplicate]

南笙酒味 提交于 2019-11-29 00:12:53
问题 This question already has answers here : Captured variable in a loop in C# (8 answers) Closed 2 years ago . I am coming from a functional-programming background at the moment, so forgive me if I do not understand closures in C#. I have the following code to dynamically generate Buttons that get anonymous event handlers: for (int i = 0; i < 7; i++) { Button newButton = new Button(); newButton.Text = "Click me!"; newButton.Click += delegate(Object sender, EventArgs e) { MessageBox.Show("I am

Anonymous methods and delegates

拜拜、爱过 提交于 2019-11-28 21:29:42
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 ? BeginInvoke((progressDelegate)delegate { bgWorker_ProgressChanged(sender, e); }); The Delegate class is the

How and when are variables referenced in Delphi's anonymous methods captured?

无人久伴 提交于 2019-11-28 20:13:36
问题 This was prompted by How to compare TFunc/TProc containing function/procedure of object?, specifically by David's comment to Barry's question. Since I don't have a Blog to post this to I'm going to ask this question here, and answer it. Question : When and how are variables referenced in Delphi's anonymous methods captured? Example: procedure ProcedureThatUsesAnonymousMethods; var V: string; F1: TFunc<string>; F2: TFunc<string>; begin F1 := function: string begin Result := V; // references

Cannot use 'this' in member initializer?

泪湿孤枕 提交于 2019-11-28 13:32:07
Is this legal? Does it contain a hidden bug or flaw? Visual studio does not give any errors or warnings but ReSharper does: /// <summary> /// immutable tuple for two /// </summary> public class Pair<TValue1, TValue2> : Singleton<TValue1> { public TValue2 Value2 { get; private set; } public Pair(TValue1 value1, TValue2 value2, Func<Pair<TValue1, TValue2>, String> toStringFunc) : this(value1, value2, () => toStringFunc(this)) { } //Red light }2> : Singleton<TValue1> I'm pretty sure I've heard that this is a compiler bug, fixed in the next release. I'm just firing up my 4.0 VM, with a simpler

How are anonymous methods implemented under the hood?

こ雲淡風輕ζ 提交于 2019-11-28 12:07:05
Does Delphi "instantiate" each anonymous method (like an object)?, if so when does Delphi create this instance, and most important, when does Delphi free it? Because anonymous method also captures external variables and extends their life time, it's important to know when these variables will be "released" from the memory. What are the possible drawbacks to declare an anonymous method inside another anonymous method. Are circular reference possible? Anonymous methods are implemented as interfaces. This article has a good explanation of how it is done by the compiler: Anonymous methods in

How to identify anonymous methods in System.Reflection

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 11:21:54
How can you identify anonymous methods via reflection? Look at the attributes of the method, and see if the method is decorated with CompilerGeneratedAttribute . Anonymous methods (as well as other objects, such as auto-implemented properties, etc) will have this attribute added. For example, suppose you have a type for your class. The anonymous methods will be in: Type myClassType = typeof(MyClass); IEnumerable<MethodInfo> anonymousMethods = myClassType .GetMethods( BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static) .Where(method => method