C# - Anonymous Method
As the name suggests, an anonymous method is a method without a name. Anonymous methods in C# can be defined using the delegate keyword and can be assigned to a variable of delegate type.
Example -
public delegate void Print(int value);
static void Main(string[] args)
{
Print print = delegate(int val) {
Console.WriteLine("Inside Anonymous method. Value: {0}", val);
};
print(100);
}
Output:
Inside Anonymous method. Value: 100
Anonymous methods can access variables defined in an outer function.
Example -
public delegate void Print(int value); static void Main(string[] args) { int i = 10; Print prnt = delegate(int val) { val += i; Console.WriteLine("Anonymous method: {0}", val); }; prnt(100); }
Output:
Anonymous method: 110Anonymous methods can also be passed to a method that accepts the delegate as a parameter.In the following example, PrintHelperMethod() takes the first parameters of the Print delegate:Example - Anonymous method with Parameter-public delegate void Print(int value); class Program { public static void PrintHelperMethod(Print printDel,int val) { val += 10; printDel(val); } static void Main(string[] args) { PrintHelperMethod(delegate(int val) {
Console.WriteLine("Anonymous method: {0}", val); }, 100); } }
Output -
Anonymous method: 110Anonymous methods can be used as event handlers:
Example - Anonymous method with Parameter as Event handlersaveButton.Click += delegate(Object o, EventArgs e){ System.Windows.Forms.MessageBox.Show("Save Successfully!"); };
Anonymous Method Limitations
- It cannot contain jump statement like goto, break or continue.
- It cannot access ref or out parameter of an outer method.
- It cannot have or access unsafe code.
- It cannot be used on the left side of the is operator.
- Anonymous method can be defined using the delegate keyword
- Anonymous method must be assigned to a delegate.
- Anonymous method can access outer variables or functions.
- Anonymous method can be passed as a parameter.
- Anonymous method can be used as event handlers.
Other Example -
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AnonymousFunction { //sample example of single cast deligate invocation class Program { delegate int SampleDeligate(int x); static void Main(string[] args) { //SampleDeligate del = Show; //Or, SampleDeligate del = new SampleDeligate(Show); Console.WriteLine(del(20)); Console.ReadLine(); } static int Show(int x) { return x; } } //Anonymous function are known as un-named method which do not have
any name defined //Anonymous function is created by using deligate class Program { delegate double CalculateAreaDeligate(int r); static void Main(string[] args) { CalculateAreaDeligate del = new CalculateAreaDeligate( delegate(int r) //unnamed method { return 3.14 * r * r; } ); Console.WriteLine(del(20)); //by lamda expression CalculateAreaDeligate del1 = r => (3.14 * r * r); Console.WriteLine(del1(40)); Console.ReadLine(); } } }
No comments:
Post a Comment