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

How does a method wait until an anonymous delegate completes?

I have the following snippet of code (as an example) that looks up a contact: public string Search() { string address = ""; ContactManager manager = new ContactManager(); // LookupComplete is just a plain event …
Chris S
  • 64,770
  • 52
  • 221
  • 239
0
votes
3 answers

When are method variables, accessible in an anonymous method, garbage collected?

For instance is it necessary to add a Timer instance to a list as I am doing here to prevent the Timer being garbage collected? Tyically if the callback was not anonymous the aswer is yes, but since it is anonymous i imagine the variables in the…
markmnl
  • 11,116
  • 8
  • 73
  • 109
0
votes
3 answers

Can you create multiple watchers in a console app, using an anonymous delegate?

In another Question I asked, I got a tip on using an anonymous delegate. The functionality works for a single watcher but when I create three it only keeps the last one. Is this because of the anonymous delegate and is there a solution to this? I…
Andy
  • 2,248
  • 7
  • 34
  • 57
0
votes
2 answers

Passing arguments to anonymous Javascript functions

Consider the code below: this.usedIds = 0; this.SendData = function(data) { var id = this.usedIds; this.usedIds++; this.xmlHttpThing.open("POST", "/Upload.aspx", true); this.xmlHttpThing.setRequestHeader("Content-type",…
Orange
0
votes
2 answers

VB.net newbie trying to convert some c# code

I've been having trouble converting the following fairly straight forward c# code into vb.net 4.0, which I understand has anonymous delegates. I just havn't been able to figure it out yet. _combo.DataBound += (sender, args) => …
Matthew Vines
  • 27,253
  • 7
  • 76
  • 97
0
votes
2 answers

Difference between EventArrivedEventHandler and EventHandler?

I tried to create a USB controller class and just tried to expose my internal EventArrivedEventHandler from ManagementEventWatcher to allow the consumer to do something if a USB is detected. I had expected to be able to cast the…
Greasyjoe
  • 95
  • 1
  • 7
0
votes
2 answers

Anonymous Method throws runtime exception when accessing local variable

Why does the following code throw the following error? private static void CreateNewAppDomain() { var cd = AppDomain.CreateDomain("CustomDomain1"); cd.DomainUnload += (sender, args) => Console.WriteLine("Domain 0 unloading, sender{0},…
Vishal Saxena
  • 137
  • 2
  • 2
  • 6
0
votes
1 answer

GetCustomAttributes on an anonymous delegate

I am struggling to retrieve custom attributes from a method. As you can see, the method ProcessXML has a custom attribute. The method itself gets passed into an anonymous delegate and then, in that context, I'm looking to get its custom attributes,…
Simon Woods
  • 2,223
  • 4
  • 27
  • 34
0
votes
0 answers

Delegates and anonymous methods

I found this in toptal.com. how come output of this is ten 10 times? delegate void Printer(); static void Main() { List printers = new List(); for (int i = 0; i < 10; i++) { printers.Add(delegate {…
aspxsushil
  • 514
  • 1
  • 5
  • 16
0
votes
1 answer

Passing property as object to method

I would like to build a helper method which will take property as object to anonymous method. This is just dummy code example to visualize problem not confront real solution which is way more complex and is not subject of this question. Some…
Patryk
  • 39
  • 7
0
votes
1 answer

Do anonymous methods inline?

First off, this is not a dupe of this question: Are anonymous methods defined inline? I think that asker is asking a different question. I am inquiring about method inlining where, during the compile process, the call to a method is replaced by the…
oscilatingcretin
  • 10,457
  • 39
  • 119
  • 206
0
votes
3 answers

Do anonymous methods passed to actions obtain data by value or reference?

I am creating an anonymous method and passing it into an action to be invoked later on. I would like to pass some numeric data (int) into my anonymous method. In order to pass the data by value, am I required to create copies? Or, will the data be…
Snoop
  • 1,046
  • 1
  • 13
  • 33
0
votes
2 answers

Convert lambda expression to anonymous method?

I am having a hard time understanding lambda expression. From my understanding lambda expression is a convenient(less codes) was of represting anonymous method. I understand anonymous method. In the below codes sample how to change the lambda…
rrene
  • 313
  • 1
  • 3
  • 14
0
votes
1 answer

Is it possible do not copy ref to local variable?

I'm trying declare ref object as optional parameter. So I've understood why I can't do that. The decesion was to overload my method and now I have a new problem: public Guid GetIdByEmployeeTypeName(string typeName) { return…
user3818229
  • 1,537
  • 2
  • 21
  • 46
0
votes
1 answer

Accessing "this" from an anonymous Java subclass

I'm trying to modify the behavior of JToolBar to allow it to dock to more than one JPanel. As part of this exercise, I need to override the method getDockingConstraint which I tried to do with an anonymous class using a definition very similar to…
Jeff Neet
  • 734
  • 1
  • 8
  • 22