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

Container to store anonymous methods

I have a following definition. type TOmniTaskDelegate = reference to procedure(const task: IOmniTask); What type of container should I use (should be supported in D2009) to store a list of TOmniTaskDelegate instances? Currently I'm using array of…
gabr
  • 26,580
  • 9
  • 75
  • 141
3
votes
2 answers

Is it possible to have an anonymous method when creating a list of items, with .NET + Linq?

i'm trying to do the following, but not sure how ... var foo = new Foo { Id = MyRandom(1, 100), Name = MyRandom(5,20), MyPets = MyRandom() ? new IList (petList => …
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
3
votes
2 answers

'Cannot capture symbol' error using an open array param inside anonymous method which is passed to another function

I'm trying to use an array of Integer parameter inside an anonymous method passed as parameter to another function: type TAnonymousMethod = reference to procedure(); procedure SubTest(AMethod : TAnonymousMethod); begin …
Fabrizio
  • 7,603
  • 6
  • 44
  • 104
3
votes
3 answers

Please guide me in getting the following line c# lambda expression code

<%= MyClass.GetData(() => new[] { Html.TextBox(prefix + "Postcode", Customer.ZipCode, new { maxlength = 7 }), Html.ValidationIcon(prefix + "ZipCode") })%> Can someone please explain me what the MyClass.GetData method is getting passed as…
HerbalMart
  • 1,669
  • 3
  • 27
  • 50
3
votes
3 answers

Is this still a closure?

The testit() method is a closure. aString has fallen out of scope but testit() can still execute on it. testit2() is using a variable that hasn't fallen out of scope (mystring) but which was also not been passed into testit2(). Is testit2()…
4thSpace
  • 43,672
  • 97
  • 296
  • 475
3
votes
1 answer

Covariance and contravariance not working when assigning anonymous method to delegate

I have the following code, taken from this MSDN: public class First { } public class Second : First { } public delegate First SampleDelegate(Second a); // Matching signature. public static First ASecondRFirst(Second first) { return new…
J. Doe
  • 1,147
  • 1
  • 13
  • 21
3
votes
2 answers

what is the need of delegates?

Iam not sure whether anyone ask the same question. But i couldnt find out any sources which are relevant for my doubt. what is the need of delegates in real time programming? there are lot of steps needs to be done to implement it. public delegate…
Tom Cruise
  • 1,395
  • 11
  • 30
  • 58
3
votes
3 answers

About in case,C# the syntax of anonymous methods simpler than syntax of lambda expressions. Book:Professional C# 5.0 and .NET 4.5.1

Recently, i read "Professional C# 5.0 and .NET 4.5.1 by Christian Nagel; Jay Glynn; Morgan Skinner" book. And i confused about: "in case,the syntax of anonymous methods simpler than syntax of lambda expressions" in book. Details in Chapter 8:…
zizifn
  • 395
  • 1
  • 2
  • 14
3
votes
2 answers

Anonymous method as function result

What I want to do is to assign an anonymous method which I get as a function result to a variable of the same type. Delphi complains about not beeing able to do the assignement. Obviously Delphi things I want to assign the "GetListener" function…
iamjoosy
  • 3,299
  • 20
  • 30
3
votes
4 answers

Scoping inside Javascript anonymous functions

I am trying to make a function return data from an ajax call that I can then use. The issue is the function itself is called by many objects, e.g.: function ajax_submit (obj) { var id = $(obj).attr('id'); var message = escape ($("#"+id+"…
DCD
  • 1,290
  • 3
  • 12
  • 20
3
votes
2 answers

Passing open array into an anonymous function

What is the least wasteful way (i.e. avoiding copying if at all possible) to pass the content of an open string array into an anonymous function and from there into another function that expects an open array? The problem is that open arrays cannot…
Ian Goldby
  • 5,609
  • 1
  • 45
  • 81
3
votes
1 answer

C# Anonymous method variable scope problem with IEnumerable

I'm trying to iterate through all components and for those who implements ISupportsOpen allow to open a project. The problem is when the anonymous method is called, then the component variable is always the same element (as coming from the outer…
theSpyCry
  • 12,073
  • 28
  • 96
  • 152
3
votes
2 answers

Ugly thing and advantage of anonymos method -C#

I was asked to explain the ugly thing and advantages of anonymous method. I explained possibly Ugly thing anonymous methods turning quickly into spaghetti code. Advantages We can produce thread safe code using anonymous method :Example static…
user274364
  • 1,797
  • 2
  • 20
  • 27
3
votes
1 answer

Why do I have generic anonymous method incompatible type?

In my studying process I use "Coding in Delphi" book by Nick Hodges. I am using Delphi 2010. In the chapter about anonymous methods, he provides a very interesting example about faking .NET using. When I try to compile the example, I get an error…
mad
  • 1,029
  • 3
  • 17
  • 38
3
votes
6 answers

C# to VB - How do I convert this anonymous method / lambda expression across?

How would you convert this to VB (using .NET 4.0 / VS2010) ? bw.DoWork += (o, args) => { Code Here }; I thought maybe like this: AddHandler bw.DoWork, Function(o, args) Code Here End Function But it says Function does not…
NibblyPig
  • 51,118
  • 72
  • 200
  • 356