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
9
votes
8 answers

Is there any overhead in the use of anonymous methods?

I would like to know if there is any overhead incurred through the use of anonymous methods when creating a Background worker. for example: public void SomeMethod() { BackgroundWorker worker = new BackgroundWorker(); worker.DoWork +=…
Lewray
  • 1,164
  • 1
  • 11
  • 26
9
votes
1 answer

How do delegate/lambda typing and coercion work?

I've noticed some examples of things that work and don't work when dealing with lambda functions and anonymous delegates in C#. What's going on here? class Test : Control { void testInvoke() { // The best overloaded method match for…
We Are All Monica
  • 13,000
  • 8
  • 46
  • 72
9
votes
1 answer

Confusion about anonymous methods parameters

While learning on anonymous methods, i've found the following example on the internet: namespace AnonymousMethods { public class MyClass { public delegate void MyDelegate(string message); //delegate accepting method with string…
kofucii
  • 7,393
  • 12
  • 51
  • 79
9
votes
2 answers

Filtering records with IEnumerable.Select

In ASP.NET MVC 4 project, I have a model for join (with payload): public class LeagueMember { [Key, Column(Order = 0)] public int MemberId { get; set; } [Key, Column(Order = 1)] public int LeagueId { get; set; } public bool?…
Annie
  • 3,090
  • 9
  • 36
  • 74
9
votes
1 answer

Assigning anonymous method to delegate using parentheses gives compiler error?

Given the following sample codes: static void SomeMethod() { Action myDelegate; //... myDelegate = delegate { Console.WriteLine( 0 ); }; myDelegate = delegate() { Console.WriteLine( 0 ); }; // compile error } What is the…
Setyo N
  • 1,953
  • 2
  • 26
  • 28
8
votes
1 answer

Delphi anonymous methods - pro and cons. Good practices when using closures(anonymus methods) in Delphi

I have a colleague in my team which is extensively using closures in our projects developed in Delphi. Personal, I don't like this because is making code harder to read and I believe that closures should be used ONLY when you need them. In the other…
RBA
  • 12,337
  • 16
  • 79
  • 126
8
votes
1 answer

Using Delphi, unable to use anonymus type as a type of a record?

I don't understand why the following small console application does not compile: program Project1; type TProc = reference to procedure; TMyRec = record Proc: TProc; end; var myProc: TProc; myRec: TMyRec; begin myProc := procedure…
RM.
  • 1,984
  • 19
  • 29
8
votes
3 answers

Problem with different "execution context" of an anonymous method within a loop

I have a problem with an anonymous method within a loop. The following code is just to illustrate my problem: private void Form1_Load(object sender, EventArgs e) { List bassists = new List(){ "Jaco Pastorius", …
Florian
  • 4,507
  • 10
  • 53
  • 73
8
votes
3 answers

How to break WinDbg in an anonymous method?

Title kinda says it all. The usual SOS command !bpmd doesn't do a lot of good without a name. Some ideas I had: dump every method, then use !bpmd -md when you find the corresponding MethodDesc not practical in real world usage, from what I can…
Richard Berg
  • 20,629
  • 2
  • 66
  • 86
8
votes
2 answers

Update ref parameter inside anonymous method

Is there a workaround to update a ref parameter inside an anonymous method? I know that the an anonymous method doesn't allow access to ref parameters of the outer scope, but is there another way to do it? I am using an external library for the…
jamos
  • 213
  • 2
  • 8
8
votes
3 answers

Cannot use 'this' in member initializer?

Is this legal? Does it contain a hidden bug or flaw? Visual studio does not give any errors or warnings but ReSharper does: /// /// immutable tuple for two /// public class Pair : Singleton { …
Maslow
  • 18,464
  • 20
  • 106
  • 193
8
votes
5 answers

Replacing a regular method with an anonymous method in C#/LINQ

I have a LINQ query that looks like this: public IEnumerable SelectFooBars() { return from f in foos join b in bars on f.BarId equals b.Id select AddMissingProp(f,…
devuxer
  • 41,681
  • 47
  • 180
  • 292
8
votes
6 answers

ok, this worked. what is it exactly?

I just lifted this snippet from a website and it proved to be exactly the solution I needed for my particular problem. I have no idea what it is (particularly the delegate and return parts) and the source doesn't explain it. Hoping SO can…
fieldingmellish
  • 1,473
  • 3
  • 16
  • 21
8
votes
2 answers

How to know if an Action is an anonymous method or not?

This is C# 4.0. I have a class that stores WeakReferences on some Actions like that: public class LoremIpsum { private Dictionary> references = new Dictionary>(); public void…
Guillaume
  • 1,782
  • 1
  • 25
  • 42
7
votes
1 answer

Implicit conversion to Func

Let's say I have an interface IMyInterface that simply describes one function: public interface IMyInterface { T MyFunction(T item); } I could just about replace this with Func, but I want the interface for semantic reasons. Can I…
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794