static void Main() { //int res = Square(2, 3, "exp"); Myclass myclass = new Myclass(); //int res = SquareUsingDelegateParameter(2, 3, specialMeth); //int res = SquareUsingDelegateParameter(2, 3, // delegate (int a, int b) // { // return 10 * (a * b); // } // ); int res = SquareUsingDelegateParameter(2, 3, (a, b) => 10 * (a * b)); //res = specialMeth(6, 9); Console.WriteLine(res); }
static void Main2(string[] args) { Myclass obj = new Myclass(); funcBinary x = null; Random r = new Random(); int num = r.Next(1, 6); switch (num) { case 1: //we are saving the function context inside the delegate variable x = new funcBinary(obj.add); //upto .net 1.1 break; case 2: x = obj.mul; break; case 3: x = obj.sub; break; case 4: x = obj.div; break; case 5: x = obj.powr; break; } //int res = x.Invoke(3, 4); //Console.WriteLine("result :" + res); x.BeginInvoke(3, 4, ProcessResult, x); Console.WriteLine("Continuing with the usual task...."); Console.ReadKey(); }
static int Square(int x, int y, string operationType) { int res = 0; Myclass myclass = new Myclass(); if (operationType == "add") { res = x * x + y * y; //myclass.add(x * x, y * y); } else if (operationType == "sub") { res = myclass.sub(x * x, y * y); } else if (operationType == "mul") { res = myclass.mul(x * x, y * y); } else if (operationType == "div") { res = myclass.div(x * x, y * y); } return(res); }