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
2
votes
2 answers

C#, invoke anonymous method (Action<>) from background thread

This should be simple! I want to create an anonymous Action<> delegate to perform a GUI update, which I will call from several other anonymous delegates (which will be run on separate threads). void Test() { Action invokeDisplay…
user1830285
  • 578
  • 8
  • 24
2
votes
1 answer

C# Abstract class, using anonymous instead of declaring concrete class?

I have an abstract class and I would like to use it quickly by NOT create a concrete class that inherit the abstract class. Well, to define the abstract method anonymously. Something like that: Command c = new Command(myObject){ …
Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
2
votes
3 answers

Use an anonymous method to avoid creating a single-use object?

I'm trying to refactor a method that parses through a file. To support files of arbitrary size, the method using a chunking approach with a fixed buffer. public int Parse() { // Get the initial chunk of data ReadNextChunk(); while…
Scott Smith
  • 3,900
  • 2
  • 31
  • 63
2
votes
2 answers

How do I pass a captured variable through a lamba expression as if it were uncaptured?

I want a piece of test code which creates a collection of tasks and then calls a method with each task's index at some random time in the near future. I wrote it like this: Random rnd = new Random(); for (Int32 taskIndex = 0; taskIndex < 10;…
TomDestry
  • 3,349
  • 5
  • 31
  • 40
2
votes
1 answer

Translating anonymous methods into lambda expression

If I would translate this anonymous method: Func f = delegate(int i) { return i + 1; }; into a lambda expression, it would like this: Func f = i => i + 1; (I know: this lambda expression will secretly generate another…
Martin Mulder
  • 12,642
  • 3
  • 25
  • 54
2
votes
3 answers

Anonymous Delegate - Search property with collection of objects

Here is the current code in my application using an anonymous delegate to search a collection on properties: public class MyObject { public MyObject() { } public string MyObjectId { get; set; } public List SubObjects { get;…
Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231
2
votes
3 answers

Do anonymous methods consume memory?

Do Anonymous Methods, Delegates, Action, Func and alike, allocate/consume/eat memory on your computer? If so, do they allocate same memory on each variable on captured code block?
Freddie Fabregas
  • 1,162
  • 1
  • 7
  • 17
2
votes
1 answer

Boxing & Unboxing: Why doesn't this delegate match?

Assuming the following delegate "caller" signature: FuncCaller(Func predicate) and a matching method: bool MyFunc(object o) When T is a reference type, I can invoke MyFunc implicitly like so: FuncCaller(MyFunc) //…
Nathan Taylor
  • 24,423
  • 19
  • 99
  • 156
2
votes
3 answers

Example and draw back of using Anonymous Methods

Can somebody give me example of how to use Anonymous Methods? Do they draw backs like performance degradation of using them?
faheem khan
  • 471
  • 1
  • 7
  • 33
2
votes
1 answer

Can you use .net 3.5 Action or Func as Marshalled unmanaged delegates?

After reading Dynamically calling unmanaged dlls in .net I've been trying to modify the code to my liking. I made a class that implements idisposable to wrap load calls in and free them when needed. However I can't seem to figure out the syntax if…
Maslow
  • 18,464
  • 20
  • 106
  • 193
2
votes
2 answers

How to subscribe to an event dynamically using anonymous methods?

I'm creating a number of UserControls dynamically using LoadControl(String) and want to subscribe to an event of each of them. All my controls inherits a common Interface that require an implementation of a common Event: public interface…
abatishchev
  • 98,240
  • 88
  • 296
  • 433
2
votes
3 answers

Anonymous methods - C# to VB.NET

I have a requirement to implement a single VB.NET instance of an application on a terminal server. To do this I am using code from the Flawless Code blog. It works well, except in the code is written in C# and uses an anonymous method which is not…
Skittles
  • 897
  • 1
  • 15
  • 24
2
votes
2 answers

Handling delegate with out parameter

I've got an delegate and event with an out parameter: public delegate void ExampleDelegate(object sender, EventArgs e, out string value); public event ExampleDelegate Example; When I'm trying to handle the event: mg.Example += (sender, e, val)…
Ralf de Kleine
  • 11,464
  • 5
  • 45
  • 87
1
vote
1 answer

can't reference non-static method from anonymous method

i need to call a non-static method from an async operation , for ease i'm using apm design , by defining a delegate assign it an anonymous method and calling beginInvoke on it . to my surprise i could not reference a non-static method from my…
eran otzap
  • 12,293
  • 20
  • 84
  • 139
1
vote
3 answers

How does one release/dispose/destroy captured variables in anonymous methods?

I am using anonymous methods to handle events in a COM object. Once the program terminates, it appears that the resources I am using in the anonymous method are not being "closed correctly" in that I get a first chance exception…
Michael Todd
  • 16,679
  • 4
  • 49
  • 69