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

Anonymous methods, scope, and serialization

Let's say I have the following code: public class Foo { private int x; private int y; public Bar CreateBar() { return new Bar(x, () => y); } } [Serializable] public class Bar { private int a; private Func
Stefan Moser
  • 6,663
  • 9
  • 35
  • 48
4
votes
1 answer

How to profile Monotouch to see count of trampolines (by type) being created at runtime?

There are quite a few posts regarding type 0, type 1 and type 2 trampolines and the runtime error of "ran out of trampolines" on iOS devices. I've got a good understanding of how to increase the number of the different trampoline types during the…
4
votes
5 answers

When using a delegate, is there a way to get hold of the object response?

As an example: WebClient.DownloadStringAsync Method (Uri) Normal code: private void wcDownloadStringCompleted( object sender, DownloadStringCompletedEventArgs e) { // The result is in e.Result string fileContent =…
balexandre
  • 73,608
  • 45
  • 233
  • 342
4
votes
2 answers

Use of anonymous method

I haven't used anonymous methods. I found a code where a list is being iterated as shown in code snippet 1. Why would the code snippet 1 be preferred over 2? List names = new List(); ... //Code snippet 1 …
softwarematter
  • 28,015
  • 64
  • 169
  • 263
4
votes
2 answers

How do I deregister an anonymous handler?

C# 2.0 has a neat feature called anonymous functions. This is intended to be used mostly with events: Button.Click += delegate(System.Object o, System.EventArgs e) { System.Windows.Forms.MessageBox.Show("Click!"); }; Now,…
Bogdan Gavril MSFT
  • 20,615
  • 10
  • 53
  • 74
4
votes
1 answer

Write anonymous expression as lambda expression

How to write the same 'Anonymous Expression' in 'Lambda Expression.' namespace AnonymouseAndLambdaExpression { // Delegate public delegate bool NumberHandler(int number); class Program { static void Main(string[] args) …
Rehan Shah
  • 1,505
  • 12
  • 28
4
votes
6 answers

Question regarding anonymous methods as class members

I am developing a PHP mini-framework, one of whose methods builds an HTML table from an array of objects: class HTMLTableField { private $hdr; private $alg; private $fun; function __construct($descr, $align, $apply) { # fun…
isekaijin
  • 19,076
  • 18
  • 85
  • 153
4
votes
4 answers

Methods and Anonymous Types

I know that you cannot return anonymous types from methods but I am wondering how the Select extension method returns an anonymous type. Is it just a compiler trick? Edit Suppose L is a List. How does this work? L.Select(s => new { Name = s }) The…
Rodrick Chapman
  • 5,437
  • 2
  • 31
  • 32
4
votes
1 answer

Can I move Delphi TThread.Synchronize() locally to a VCL form to be called from both a main or worker thread?

I am using CreateAnonymousThread for a worker task, and when I started with it I used Synchronize within the entire declaration as per documented examples, e.g: procedure Txxx.RunWorker; begin FExecutionThread :=…
Brian Frost
  • 13,334
  • 11
  • 80
  • 154
4
votes
3 answers

Predicate match question

I do not understand how following code works. Specifically, I do not understand using of "return i<3". I would expect return i IF its < than 3. I always though that return just returns value. I could not even find what syntax is it. Second question,…
Petr
  • 7,787
  • 14
  • 44
  • 53
4
votes
3 answers

What generic constraint do I use for an anonymous method type?

I would like to declare a generic record like so: type TMyDelegate = record private fDelegate: T; public class operator Implicit(a: T): TMyDelegate; class operator Implicit(A: TMyDelegate: T); end; I'd like to…
Johan
  • 74,508
  • 24
  • 191
  • 319
4
votes
1 answer

Does a TList.Clear free all captured variables?

When I have a TList (so, a list of "reference to procedure"), and I Clear it, do all the captured variables used in the anonymous methods get freed, so no leaking occurs? Ie. is reference counting in effect upon clearing the TList?
Domus
  • 1,263
  • 7
  • 23
3
votes
3 answers

Setting parameter values of anonymous function in C#

Lets say i have the following code private Func _method; public void SetExecutableMethod(Func methodParam) { _method = methodParam; } public T ExecuteMethod(object[] parameterValues) { //get the number of parameters _method has; …
mike01010
  • 5,226
  • 6
  • 44
  • 77
3
votes
4 answers

How to determine anonymous function parameters in c#?

Given the following code, public T Execute(Func methodParam) { return methodParam (); } public void CallMethodsAnonymously() { T result = Execute(() => _service.SomeMethod1()); T result1 =…
mike01010
  • 5,226
  • 6
  • 44
  • 77
3
votes
1 answer

Using List(Of T).ForEach method to update values not working as expected

I have the following code, PB.ForEach(Function(x) x.Cost = GetPartCost(x.PartNumber, x.Units, x.Cost, FB)) Return PB.Sum(Function(x) (x.Cost * x.Qty)) However it always returns 0. I've checked and the GetPartCost function executes and returns a…
Kratz
  • 4,280
  • 3
  • 32
  • 55