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

Why is an out parameter not allowed within an anonymous method?

This is not a dupe of Calling a method with ref or out parameters from an anonymous method I am wondering why out parameters are not allowed within anonymous methods. Not allowing ref parameters makes a bit more sense to me, but the out parameters,…
Chris Ballance
  • 33,810
  • 26
  • 104
  • 151
14
votes
3 answers

Anonymous methods and delegates

I try to understand why a BeginInvoke method won't accept an anonymous method. void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { if (InvokeRequired) { //Won't compile BeginInvoke(delegate(object…
Mez
  • 2,817
  • 4
  • 27
  • 29
14
votes
2 answers

Why Local Functions generate IL different from Anonymous Methods and Lambda Expressions?

Why the C# 7 Compiler turns Local Functions into methods within the same class where their parent function is. While for Anonymous Methods (and Lambda Expressions) the compiler generates a nested class for each parent function, that will contain all…
KeyBored
  • 601
  • 4
  • 14
13
votes
2 answers

How are anonymous methods implemented under the hood?

Does Delphi "instantiate" each anonymous method (like an object)?, if so when does Delphi create this instance, and most important, when does Delphi free it? Because anonymous method also captures external variables and extends their life time,…
zeus
  • 12,173
  • 9
  • 63
  • 184
13
votes
6 answers

C#: Is it possible to declare a local variable in an anonymous method?

Is is possible to have a local variable in an anonymous c# methods, i.e. in the following code I would like to perform the count only once. IQueryable linq = db.Enquiries; if(...) linq = linq.Where(...); if(...) linq = linq.Where(e => …
Niels Bosma
  • 11,758
  • 29
  • 89
  • 148
13
votes
7 answers

Is there a case where delegate syntax is preferred over lambda expression for anonymous methods?

With the advent of new features like lambda expressions (inline code), does it mean we dont have to use delegates or anonymous methods anymore? In almost all the samples I have seen, it is for rewriting using the new syntax. Any place where we still…
dragon
  • 247
  • 2
  • 4
  • 16
12
votes
3 answers

Private field captured in anonymous delegate

class A { public event EventHandler AEvent; } class B { private A _foo; private int _bar; public void AttachToAEvent() { _foo.AEvent += delegate() { ... UseBar(_bar); ... } } } Since…
Damir
  • 345
  • 4
  • 16
12
votes
3 answers

Delegates and Lambdas and LINQ, Oh My!

As a fairly junior developer, I'm running into a problem that highlights my lack of experience and the holes in my knowledge. Please excuse me if the preamble here is too long. I find myself on a project that involves my needing to learn a number…
Steve Brouillard
  • 3,256
  • 5
  • 41
  • 60
12
votes
3 answers

EventHandlers and Anonymous Delegates / Lambda Expressions

I'm hoping to clear some things up with anonymous delegates and lambda expressions being used to create a method for event handlers in C#, for myself at least. Suppose we have an event that adds either an anonymous delegate or a lambda expression…
Nicholas Mancuso
  • 11,599
  • 6
  • 45
  • 47
12
votes
5 answers

C# 2.0 Threading Question (anonymous methods)

I have a simple application with the following code: FileInfo[] files = (new DirectoryInfo(initialDirectory)).GetFiles(); List threads = new List(files.Length); foreach (FileInfo f in files) { Thread t = new…
John
  • 17,163
  • 16
  • 65
  • 83
12
votes
5 answers

Creating two delegate instances to the same anonymous method are not equal

Consider the following example code: static void Main(string[] args) { bool same = CreateDelegate(1) == CreateDelegate(1); } private static Action CreateDelegate(int x) { return delegate { int z = x; }; } You would imagine that the two…
user173231
11
votes
4 answers

Scope of anonymous methods

One nice thing about anonymous methods is that I can use variables that are local in the calling context. Is there any reason why this does not work for out-parameters and function results? function ReturnTwoStrings (out Str1 : String) :…
jpfollenius
  • 16,456
  • 10
  • 90
  • 156
11
votes
2 answers

Discrete Anonymous methods sharing a class?

I was playing a bit with Eric Lippert's Ref class from here. I noticed in the IL that it looked like both anonymous methods were using the same generated class, even though that meant the class had an extra variable. While using only one new…
Brian
  • 25,523
  • 18
  • 82
  • 173
11
votes
5 answers

C#: Anonymous method vs Named method

I'm new to SO and programming and learning day by day with bits and pieces of tech (C#) jargons. After Googling for a while, below is what I've researched about methods A Method is a block of statements, which serves for code reusability & it also…
c-sharp user
  • 228
  • 3
  • 9
10
votes
1 answer

C# -Closure -Clarification

I am learning C#.Can I mean closure as a construct that can adopt the changes in the environment in which it is defined. Example : List gurus = new List() { new Person{id=1,Name="Jon Skeet"}, …
user274364
  • 1,797
  • 2
  • 20
  • 27