Questions tagged [anonymous-methods]

An anonymous method is a procedure or function that does not have a name associated with it.

An anonymous method is a procedure or function that does not have a name associated with it. An anonymous method treats a block of code as an entity that can be assigned to a variable or used as a parameter to a method. In addition, an anonymous method can refer to variables and bind values to the variables in the context in which the method is defined. They are similar to the construct of closures defined in some languages.

An anonymous method in C# is a way to pass a code block as a delegate parameter.

320 questions
31
votes
7 answers

Can someone explain Anonymous methods to me?

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?
Steve
  • 6,382
  • 3
  • 41
  • 66
30
votes
2 answers

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

See the following code: public abstract class Base { public virtual void Foo() where T : class { Console.WriteLine("base"); } } public class Derived : Base { public override void Foo() { …
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
28
votes
4 answers

BackgroundWorker with anonymous methods?

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…
Mohammad Dayyan
  • 21,578
  • 41
  • 164
  • 232
28
votes
3 answers

Anonymous methods vs. lambda expression

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 =…
sarepta
  • 348
  • 1
  • 3
  • 11
26
votes
5 answers

ThreadPool.QueueUserWorkItem with a lambda expression and anonymous method

Passing two parameters to a new thread on the threadpool can sometimes be complicated, but it appears that with lambda expressions and anonymous methods, I can do this: public class TestClass { public void DoWork(string s1, string s2) { …
Scott Whitlock
  • 13,739
  • 7
  • 65
  • 114
26
votes
2 answers

Lambda expression vs anonymous methods

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…
lojol
  • 471
  • 1
  • 5
  • 9
26
votes
4 answers

C# - anonymous functions and event handlers

I have the following code: public List FindStepsByType(IWFResource res) { List retval = new List(); this.FoundStep += delegate(object sender, WalkerStepEventArgs e) …
Adi Barda
  • 3,259
  • 11
  • 32
  • 33
24
votes
1 answer

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

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:…
Cosmin Prund
  • 25,498
  • 2
  • 60
  • 104
23
votes
5 answers

Closures in C# event handler delegates?

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++) { …
erjiang
  • 44,417
  • 10
  • 64
  • 100
23
votes
2 answers

Wrong code when combining anonymous and nested procedures

I've got some unexpected access violations for Delphi code that I think is correct, but seems to be miscompiled. I can reduce it to procedure Run(Proc: TProc); begin Proc; end; procedure Test; begin Run( procedure var S: PChar; …
user743382
23
votes
3 answers

Self-invoking anonymous functions

In JavaScript, it's not uncommon to see self-invoking functions: var i = (function(x) { return x; })(42); // i == 42 While I'm certainly not comparing the languages, I figured such a construct would be translatable to C#, provided a language…
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
23
votes
2 answers

Dynamically assigning function implementation in Python

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…
Yarin
  • 173,523
  • 149
  • 402
  • 512
22
votes
6 answers

Calling newly defined method from anonymous class

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();
user1544745
  • 677
  • 2
  • 8
  • 15
21
votes
1 answer

VB.NET RemoveHandler & Anonymous Methods

How do I use RemoveHandler with anonymous methods? This is how I add a handler for MyEvent event of the class MyClass: AddHandler MyClass.MyEvent, Sub() '... End Sub How do I then use…
acermate433s
  • 2,514
  • 4
  • 24
  • 32
21
votes
4 answers

Convert this delegate to an anonymous method or lambda

I am new to all the anonymous features and need some help. I have gotten the following to work: public void FakeSaveWithMessage(Transaction t) { t.Message = "I drink goats blood"; } public delegate void FakeSave(Transaction t); public void…
George Mauer
  • 117,483
  • 131
  • 382
  • 612
1
2
3
21 22