//-------------------------------------------------- static void Main(string[] args) { //using the delegate Console.WriteLine("__________________________________________"); Console.WriteLine(">>>>>calling delegate..."); MathOp f = add; var res = f(2, 3); Debug.Assert(res == 5); Console.WriteLine("f(2+3)={0}", res); Console.WriteLine("__________________________________________"); Console.WriteLine(">>>>>calling delegate..."); f = subtract; res = f(100, 90); Debug.Assert(res == 10); Console.WriteLine("f(100-90)={0}", res); //using the CalculateAndPrint //using the delegate Console.WriteLine("__________________________________________"); Console.WriteLine(">>>>>passing delegate as parameter..."); f = add; CalculateAndPrint(2, 3, f); Console.WriteLine("__________________________________________"); Console.WriteLine(">>>>>passing delegate as parameter..."); f = subtract; CalculateAndPrint(100, 90, f); //call using the DELEGATE INLINED ! //return type can be deduced !!! Console.WriteLine("__________________________________________"); Console.WriteLine(">>>>>using delegate - inline..."); CalculateAndPrint(2, 3, delegate(int a, int b){ return(a + b); }); Console.WriteLine("__________________________________________"); Console.WriteLine(">>>>>using delegate - inline..."); CalculateAndPrint(100, 10, delegate(int a, int b) { return(a - b); } ); //using lambdas - to inline method to call - to define the method (delegate parameter) Console.WriteLine("__________________________________________"); Console.WriteLine(">>>>>using lambdas..."); CalculateAndPrint(2, 3, (int a, int b) => { return(a + b); }); Console.WriteLine("__________________________________________"); Console.WriteLine(">>>>>using lambdas..."); CalculateAndPrint(100, 90, (int a, int b) => { return(a - b); }); //using delegate + generics Console.WriteLine("__________________________________________"); Console.WriteLine(">>>>>calling delegate generic..."); CombineGeneric <int> gn = add; res = gn(2, 3); Debug.Assert(res == 5); Console.WriteLine("gn(2+3)={0}", res); Console.WriteLine("__________________________________________"); Console.WriteLine(">>>>>calling delegate generic (double)..."); CombineGeneric <double> gnd = add; double resd = gnd(2.5, 3.5); Console.WriteLine("gn(2.5+3.5)={0}", resd); Console.WriteLine("__________________________________________"); Console.WriteLine(">>>>>using CalculateAndPrintGen<int>..."); CalculateAndPrintGen <int>(100, 90, (a, b) => { return(a + b); }); Console.WriteLine("__________________________________________"); Console.WriteLine(">>>>>using CalculateAndPrintGen<double>..."); CalculateAndPrintGen <double>(10.0, 20.0, (a, b) => a + b); Console.WriteLine("__________________________________________"); Console.WriteLine(">>>>>using CalculateAndPrintGen - boolean..."); CalculateAndPrintGen(true, false, (a, b) => a && b); }
static void CalculateAndPrintGen <T>(T x, T y, CombineGeneric <T> f) { var res = f(x, y); Console.WriteLine("CalculateAndPrintGen({0},{1})={2}; f={3}", x, y, res, f.Method.ToString()); }