Anonymous methods are “name-less methods”, that can be invoked by using the delegate variable or an event.
Event with Anonymous Method:
EventName += delegate(param1, param2, …)
{
//method body here
}
Anonymous methods can be used anywhere within the method, to create methods instantly, without define a method at the class level.
Guidelines:
- It can’t be called without a delegate or event.
- It can’t contain jump statements like go to, break, continue.
- It can access local variables and parameters of outer method.
- It can be passed as a parameter to any method; in this case, the delegate acts as data type for the anonymous method.
- It can’t access ref or out parameter of an outer method.
- It is mainly used for event handlers.