Could someone explain what anonymous methods are in C# (in simplistic terms) and provide examples in possible please
4 Answers
Anonymous methods were introduced into C# 2 as a way of creating delegate instances without having to write a separate method. They can capture local variables within the enclosing method, making them a form of closure.
An anonymous method looks something like:
delegate (int x) { return x * 2; }
and must be converted to a specific delegate type, e.g. via assignment:
Func<int, int> foo = delegate (int x) { return x * 2; };
... or subscribing an event handler:
button.Click += delegate (object sender, EventArgs e) {
// React here
};
For more information, see:
- My article (written a long time ago) on delegate changes in C# 2
- MSDN on anonymous methods
- Chapter 5 of C# in Depth if you fancy buying my book :)
Note that lamdba expressions in C# 3 have almost completely replaced anonymous methods (although they're still entirely valid of course). Anonymous methods and lambda expressions are collectively described as anonymous functions.

- 1,421,763
- 867
- 9,128
- 9,194
-
It is also worth adding that anonymous methods on exists in the local scope so that means they cannot be shared elsewhere in the program or exposed out the assembly. If you need to reuse the code you need to write a dedicate method that can be called. – Damian Apr 07 '14 at 20:49
-
@Damian: I don't think it's worth calling that out - I can't remember that ever coming up as something someone has *wanted* to do (unlike anonymous *types*). I think it's obvious just from the syntax. – Jon Skeet Apr 07 '14 at 20:50
Anonymous method is method that simply doesn't have name and this method is declared in place, for example:
Button myButton = new Button();
myButton .Click +=
delegate
{
MessageBox.Show("Hello from anonymous method!");
};

- 15,573
- 9
- 52
- 68
-
Is this implicitly an Action? If I would assign this to a variable and then add the variable to `myButton.Click` I would have to use EventHandler for the variable type even though it doesn't use the parameters. – Arlen Beiler Aug 22 '13 at 14:33
An anonymous method is a block of code that is used where a method would usually be required and which does not have a name (hence anonymous).
MSDN has examples of using anonymous methods.

- 24,780
- 5
- 50
- 61
-
so you would not need to create a class then? And I assume the method can be called from any other method? – James May 15 '11 at 11:35
-
@James - No, you don't need to create a class to hold the anonymous method. The method can be called from anywhere it's passed to - you can't call it at a later date without a reference to it - it's anonymous. – Will A May 15 '11 at 11:40
-
1Thanks, can you specify a case where an anonymous method might might be required? – James May 15 '11 at 11:41
-
@James - I have to admit that C# isn't my language of choice, so I've not actually _used_ anonymous methods. Am sure someone else can answer this, though. – Will A May 15 '11 at 11:42
These are methods without name.
For example, ordinary method is:
public void Foo()
{
Console.WriteLine("hello");
}
While anonymous method can be:
myList.ForEach(item => Console.WriteLine("Current item: " + item));
The code inside the ForEach
is actually a method but has no name and you can't call it from the outside.

- 66,030
- 26
- 140
- 208
-
4That's not an anonymous method - it's a lamdba expression. Lambda expressions and anonymous methods are collectively called anonymous functions. – Jon Skeet May 15 '11 at 12:03
-
Thanks @Jon I just wanted to stick with simple words per the OP request "in simplistic terms". Cheers for 300K! ;) – Shadow The GPT Wizard May 15 '11 at 12:14