anonymous-methods

Is there a useful design pattern for chained asynchronous/event calls?

不问归期 提交于 2019-12-07 22:54:00
问题 I'm currently having to integrate a lot of web service calls inside Silverlight that make calls similar to the code below. No user interaction should be possible until the loading of all 3 is complete. // In my view, having standard delegate methods (non-anonymous) makes the // code below even messier. // I've left out the EventArgs to shorten the example however // I usually use these objects in the delegate method. MyService1 service1 = new MyService1(); service1.GetAllUsersCompleted +=

Is there a useful design pattern for chained asynchronous/event calls?

断了今生、忘了曾经 提交于 2019-12-06 10:57:40
I'm currently having to integrate a lot of web service calls inside Silverlight that make calls similar to the code below. No user interaction should be possible until the loading of all 3 is complete. // In my view, having standard delegate methods (non-anonymous) makes the // code below even messier. // I've left out the EventArgs to shorten the example however // I usually use these objects in the delegate method. MyService1 service1 = new MyService1(); service1.GetAllUsersCompleted += delegate { MyService2 service2 = new MyService2(); service2.AnotherTaskCompleted += delegate { MyService3

Question regarding anonymous methods as class members

允我心安 提交于 2019-12-05 16:24:14
I am developing a PHP mini-framework, one of whose methods builds an HTML table from an array of objects: class HTMLTableField { private $hdr; private $alg; private $fun; function __construct($descr, $align, $apply) { # fun must be an anonymous function $this->hdr = '<th>' . htmlentities($descr) . "</th>\n"; $this->alg = "<td style=\"text-align: {$align}\">"; $this->fun = $apply; } function getHeader() { return $this->hdr; } function getCell($row) { # This line fails return "{$this->alg}{$this->fun($row)}</td>"; } } function gen_html_table($rows, $fields) { # $fields must be an array of

Serializing a list of anonymous delegates

杀马特。学长 韩版系。学妹 提交于 2019-12-05 02:39:46
问题 This question may be very similar to my one, but I cannot see the answer I need in it. I have a class, called CASM , that has a List<Action> . I want to serialize this class (using the BinaryFormatter or something similar). This class and all classes referenced in the Action s have got correct [Serializable] and [NonSerializable] attributes. The problem comes when serialization is attempted - it gives this error: Type 'CASM.CASM+<>c__DisplayClass2c' in Assembly 'CASM, Version=1.0.0.0, Culture

Why is an out parameter not allowed within an anonymous method?

蓝咒 提交于 2019-12-04 16:49:22
问题 This is not a dupe of Calling a method with ref or out parameters from an anonymous method I am wondering why out parameters are not allowed within anonymous methods. Not allowing ref parameters makes a bit more sense to me, but the out parameters, not as much. what are your thoughts on this 回答1: In some ways this is a dupe. Out parameters are ref parameters. There is simply an extra attribute on the value that is used by the C# language. The reason for disallowing them is the exact same as

Why don't anonymous delegates/lambdas infer types on out/ref parameters?

こ雲淡風輕ζ 提交于 2019-12-04 10:28:15
问题 Several C# questions on StackOverflow ask how to make anonymous delegates/lambdas with out or ref parameters. See, for example: Calling a method with ref or out parameters from an anonymous method Write a lambda or anonymous function that accepts an out parameter To do so, you just need to specify the type of the parameter, as in: public void delegate D(out T p); // ... D a = (out T t) => { ... }; // Lambda syntax. D b = delegate(out T t) { ... }; // Anonymous delegate syntax. What I'm

BackgroundWorker with anonymous methods?

六眼飞鱼酱① 提交于 2019-12-04 08:51:58
问题 I'm gonna create a BackgroundWorker with an anonymous method. I've written the following code : BackgroundWorker bgw = new BackgroundWorker(); bgw.DoWork += new DoWorkEventHandler( () => { int i = 0; foreach (var item in query2) { .... .... } } ); But Delegate 'System.ComponentModel.DoWorkEventHandler' does not take '0' arguments and I have to pass two objects to the anonymous method : object sender, DoWorkEventArgs e Could you please guide me, how I can do it ? Thanks. 回答1: You just need to

Anonymous methods vs. lambda expression [duplicate]

ぐ巨炮叔叔 提交于 2019-12-04 07:54:34
问题 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) Closed 5 years ago . 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

Is there any overhead in the use of anonymous methods?

时光怂恿深爱的人放手 提交于 2019-12-04 05:26:31
I would like to know if there is any overhead incurred through the use of anonymous methods when creating a Background worker. for example: public void SomeMethod() { BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += (sender, e) => { //large amount of code } worker.RunWorkerAsync(); } Would the example above be any better or worse than defining the //large amount of code in a separate method? Is there any overhead incurred in defining the background worker method in-line, particularly if SomeMethod() is called often? There is a small difference in how named methods and

Why does CLR create new class for anonymous method?

家住魔仙堡 提交于 2019-12-04 01:55:54
问题 I am using anonymous functions in my projects no less. And till know I was thinking that, C# compiler generates just a method using the code used for the anonymous method in the same class . But, after decompiling this code in IL, I saw that CLR created a new class. public class Comparer { public delegate int Greater(int a, int b); public int Great(Greater greater, int a, int b) { return greater(a, b); } } static void Main(string[] args) { int valueOfA = 11, valueOfB = 23, valueOfC = 42;