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
21
votes
5 answers

Why can I not edit a method that contains an anonymous method in the debugger?

So, every time I have written a lambda expression or anonymous method inside a method that I did not get quite right, I am forced to recompile and restart the entire application or unit test framework in order to fix it. This is seriously annoying,…
Eyvind
  • 5,221
  • 5
  • 40
  • 59
20
votes
10 answers

Avoid or embrace C# constructs which break edit-and-continue?

I develop and maintain a large (500k+ LOC) WinForms app written in C# 2.0. It's multi-user and is currently deployed on about 15 machines. The development of the system is ongoing (can be thought of as a perpetual beta), and there's very little done…
Bradley Smith
  • 13,353
  • 4
  • 44
  • 57
19
votes
5 answers

DataTable.Select vs DataTable.rows.Find vs foreach vs Find(Predicate)/Lambda

I have a DataTable/collection that is cached in memory, I want to use this as a source to generate results for an auto complete textbox (using AJAX of course). I am evaluating various options to fetch the data quickly. The number of items in the…
Binoj Antony
  • 15,886
  • 25
  • 88
  • 96
19
votes
6 answers

Anonymous method as parameter to BeginInvoke?

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 =…
Charles Bretana
  • 143,358
  • 22
  • 150
  • 216
18
votes
1 answer

How to check if two method references are referencing same method?

I am trying to make list of event handlers where handler is method reference. To delete specific handler i need to find it in the list. But how can i compare code address of two method references? type TEventHandler = reference to…
Andrei Galatyn
  • 3,322
  • 2
  • 24
  • 38
17
votes
2 answers

Are anonymous listeners incompatible with weak references?

I was reading this question that just got asked: Avoid memory leaks in callbacks? And I was quite confused, until someone answered the following: "The problem with this approach is you cannot have a listener which is only referenced in the…
Cedric Martin
  • 5,945
  • 4
  • 34
  • 66
17
votes
3 answers

Interfaces, Anonymous Methods and Memory Leaks

this is a constructed example. I don't want to post the original code here. I tried to extract the relevant parts though. I have an interface that manages a list of listeners. TListenerProc = reference to procedure (SomeInt :…
jpfollenius
  • 16,456
  • 10
  • 90
  • 156
16
votes
3 answers

VCL events with anonymous methods - what do you think about this implementation?

Since anonymous methods appeared in Delphi I wanted to use them in VCL components events. Obviously for backward compatibility the VCL wasn't updated, so I managed to make a simple implementation with a few caveats. type TNotifyEventDispatcher =…
pragmatic_programmer
  • 3,566
  • 3
  • 29
  • 36
16
votes
2 answers

How to assign a function, returned by another function, to a function variable? The result rather than the generating function itself

A function is returning an anonymous function. I would like to assign the result to a variable. However the compiler thinks that I am trying to assign the function and not the result of the function. How can I resolve this? program…
RM.
  • 1,984
  • 19
  • 29
16
votes
2 answers

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

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…
John Feminella
  • 303,634
  • 46
  • 339
  • 357
15
votes
6 answers

Does VB.NET have anonymous functions?

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…
Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
15
votes
3 answers

Memory leaks happens in nested anonymous method

In Delphi XE, the following code will cause memory leak: procedure TForm1.Button1Click(Sender: TObject); var P, B: TProc; begin B := procedure begin end; P := procedure begin B; end; end; Run the code…
Chau Chee Yang
  • 18,422
  • 16
  • 68
  • 132
15
votes
1 answer

LINQ vs Lambda vs Anonymous Methods vs Delegates

Can anyone explain what are the LINQ, Lambda, Anonymous Methods, Delegates meant? How these 3 are different for each other? Was one replaceable for another? I didn't get any concrete answer when i did Googling
Gopi
  • 5,656
  • 22
  • 80
  • 146
15
votes
4 answers

Assigning property of anonymous type via anonymous method

I am new in the functional side of C#, sorry if the question is lame. Given the following WRONG code: var jobSummaries = from job in jobs where ... select new { ID =…
GarbageGuy
  • 513
  • 1
  • 5
  • 12
15
votes
3 answers

Assign an anonymous method to an interface variable or parameter?

Anonymous methods are essentially interfaces with an Invoke method: type TProc = reference to procedure; IProc = interface procedure Invoke; end; Now, is there a possibility to assign them to an actual interface variable or pass them as…
Max
  • 671
  • 5
  • 13
1 2
3
21 22