Anonymous methods in c#:-
What is an anonymous method?
In simple terms, anonymous method is a method without a name.
Anonymous methods provide a technique to pass a code block as a delegate parameter.
You need not specify the return type in an anonymous method; it is inferred from the return statement inside the method body.
Remarks:-
In simple terms, anonymous method is a method without a name.
Anonymous methods provide a technique to pass a code block as a delegate parameter.
You need not specify the return type in an anonymous method; it is inferred from the return statement inside the method body.
- In versions of C# before 2.0, the only way to declare a delegate was to use named methods.
- C# 2.0 introduced anonymous methods and in C# 3.0 and later, lambda expressions supersede anonymous methods as the preferred way to write inline code.
- However, the information about anonymous methods in this topic also applies to lambda expressions. There is one case in which an anonymous method provides functionality not found in lambda expressions. Anonymous methods enable you to omit the parameter list. This means that an anonymous method can be converted to delegates with a variety of signatures. This is not possible with lambda expressions.
Writing an Anonymous Method
Anonymous methods are declared with the creation of the delegate instance, with a delegate keyword. For example,
delegate void NumberChanger(int n); ... NumberChanger nc = delegate(int x) { Console.WriteLine("Anonymous Method: {0}", x); };
The code block Console.WriteLine("Anonymous Method: {0}", x); is the body of the anonymous method.
The delegate could be called both with anonymous methods as well as named methods in the same way, i.e., by passing the method parameters to the delegate object.
For example,
nc(3033);
Example
The following example demonstrates the concept:
using System; delegate void NumberChanger(int n); namespace DelegateAppl { class TestDelegate { static int num = 10; public static void AddNum(int p) { num += p; Console.WriteLine("Named Method: {0}", num); } public static void MultNum(int q) { num *= q; Console.WriteLine("Named Method: {0}", num); } public static int getNum() { return num; } static void Main(string[] args) { //create delegate instances using anonymous method NumberChanger nc = delegate(int x) { Console.WriteLine("Anonymous Method: {0}", x); }; //calling the delegate using the anonymous method nc(10); //instantiating the delegate using the named methods nc = new NumberChanger(AddNum); //calling the delegate using the named methods nc(5); //instantiating the delegate using another named methods nc = new NumberChanger(MultNum); //calling the delegate using the named methods nc(2); Console.ReadKey(); } } }
When the above code is compiled and executed, it produces the following result:
Anonymous Method: 10 Named Method: 15 Named Method: 30
Remarks:-
- The scope of the parameters of an anonymous method is the anonymous-method-block.
- It is an error to have a jump statement, such as goto, break, or continue, inside the anonymous method block if the target is outside the block. It is also an error to have a jump statement, such as goto, break, or continue, outside the anonymous method block if the target is inside the block.
- The local variables and parameters whose scope contains an anonymous method declaration are called outer variables of the anonymous method. For example, in the following code segment, n is an outer variable:
- A reference to the outer variable n is said to be captured when the delegate is created. Unlike local variables, the lifetime of a captured variable extends until the delegates that reference the anonymous methods are eligible for garbage collection.
- An anonymous method cannot access the ref or out parameters of an outer scope.
- No unsafe code can be accessed within the anonymous-method-block.
- Anonymous methods are not allowed on the left side of the is operator.