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
1855
votes
4 answers

Is there a reason for C#'s reuse of the variable in a foreach?

When using lambda expressions or anonymous methods in C#, we have to be wary of the access to modified closure pitfall. For example: foreach (var s in strings) { query = query.Where(i => i.Prop == s); // access to modified closure ... } Due…
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
252
votes
14 answers

Unsubscribe anonymous method in C#

Is it possible to unsubscribe an anonymous method from an event? If I subscribe to an event like this: void MyMethod() { Console.WriteLine("I did it!"); } MyEvent += MyMethod; I can un-subscribe like this: MyEvent -= MyMethod; But if I…
Eric
  • 4,201
  • 5
  • 27
  • 36
193
votes
6 answers

delegate keyword vs. lambda notation

Once it is compiled, is there a difference between: delegate { x = 0; } and () => { x = 0 } ?
MojoFilter
  • 12,256
  • 14
  • 53
  • 61
135
votes
9 answers

Anonymous method in Invoke call

Having a bit of trouble with the syntax where we want to call a delegate anonymously within a Control.Invoke. We have tried a number of different approaches, all to no avail. For example: myControl.Invoke(delegate() { MyMethod(this, new…
Duncan
  • 10,218
  • 14
  • 64
  • 96
94
votes
5 answers

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

I thought it would be nice to do something like this (with the lambda doing a yield return): public IList Find(Expression> expression) where T : class, new() { IList list = GetList(); var fun =…
Lance Fisher
  • 25,684
  • 22
  • 96
  • 122
73
votes
9 answers

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

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…
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
65
votes
6 answers

Adding and Removing Anonymous Event Handler

I was wondering if this actually worked ? private void RegisterKeyChanged(T item) { item.OnKeyChanged += (o, k) => ChangeItemKey((T)o, k); } private void UnRegisterKeyChanged(T item) { item.OnKeyChanged -= (o, k) => ChangeItemKey((T)o,…
HaxElit
  • 3,983
  • 7
  • 34
  • 43
62
votes
6 answers

Can an anonymous method in C# call itself?

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…
Matt
  • 25,943
  • 66
  • 198
  • 303
57
votes
9 answers

When not to use lambda expressions

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…
Binoj Antony
  • 15,886
  • 25
  • 88
  • 96
43
votes
6 answers

How can I use continue statement in .ForEach() method

Is there an equivalent to the continue statement in ForEach method? List lst = GetIdList(); lst.ForEach(id => { try { var article = GetArticle(id); if (article.author.contains("Twain")) { //want to…
Laguna
  • 3,706
  • 5
  • 27
  • 42
41
votes
3 answers

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

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…
Dax Fohl
  • 10,654
  • 6
  • 46
  • 90
38
votes
7 answers

How do I Unregister 'anonymous' event handler

Say if I listen for an event: Subject.NewEvent += delegate(object sender, NewEventArgs e) { //some code }); Now how do I un-register this event? Or just allow the memory to leak?
P.K
  • 18,587
  • 11
  • 45
  • 51
35
votes
8 answers

How to yield return inside anonymous methods?

Basically I have an anonymous method that I use for my BackgroundWorker: worker.DoWork += ( sender, e ) => { foreach ( var effect in GlobalGraph.Effects ) { // Returns EffectResult yield return image.Apply (effect); …
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
34
votes
6 answers

anonymous delegates in C#

I can't be the only one getting tired of defining and naming a delegate for just a single call to something that requires a delegate. For example, I wanted to call .Refresh() in a form from possibly other threads, so I wrote this code: private void…
Nefzen
  • 7,819
  • 14
  • 36
  • 34
33
votes
4 answers

Why can't c# use inline anonymous lambdas or delegates?

I hope I worded the title of my question appropriately. In c# I can use lambdas (as delegates), or the older delegate syntax to do this: Func fnHello = () => "hello"; Console.WriteLine(fnHello()); Func fnHello2 = delegate() { …
Samuel Meacham
  • 10,215
  • 7
  • 44
  • 50
1
2 3
21 22