static void Main() { //3. invoke the delegate to invoke the method(s) //calDelAdd.Invoke(12, 13); //or //calDelMulti(12, 13); Downloader downloader = new Downloader(); //downloader.Download; OperationDel downloadDel = new OperationDel(downloader.Download); StatusDel successDel = new StatusDel(Program.NotifyOnSuccess); StatusDel failureDel = new StatusDel(Program.NotifyOnFailure); Operate(downloadDel, "http://www.google.com", successDel, failureDel); //2. creat an instance of the delegate Calculation calculation = new Calculation(); //InvokeCalculationMethods(calculation); CalDel calDelAdd = new CalDel(calculation.Add); InvokeCalculationMethods(calDelAdd); CalDel calDelMulti = new CalDel(Calculation.Multiply); InvokeCalculationMethods(calDelMulti); }
static void Main(string[] args) { int val = 2; OperationDel someDel = subtract2; someDel += add1; OperationDel myDel = someDel; OperationDel mult2 = delegate(ref int a) { a *= 2; }; processAndShow(myDel, val); //1 myDel += mult2; processAndShow(myDel, val); //2 myDel += subtract2; processAndShow(myDel, val); //0 myDel -= subtract2; processAndShow(myDel, val); //2 myDel += delegate(ref int a) { a *= 2; }; processAndShow(myDel, val); //4 myDel -= delegate(ref int a) { a *= 2; }; myDel -= mult2; myDel -= subtract2; processAndShow(myDel, val); //6 myDel -= mult2; processAndShow(myDel, val); //6 }
static void Main(string[] args) { //singlecast Delagtes //creating object of del OperationDel del1 = new OperationDel(sum); OperationDel del2 = Multiply; //del1(3, 4); //invoke as next line //del1.Invoke(8, 9); //del2(4, 5); //multicast Delagtes rectDelegate rectdelObj = new rectDelegate(area); rectdelObj += perimeter; rectdelObj += traingleArea; rectdelObj.Invoke(6.3, 4.2); rectdelObj -= traingleArea; rectdelObj.Invoke(6.3, 4.2); //Anonymous methods mydelgate print = delegate(int val) { Console.WriteLine("Inside Anonymous method. Value: {0}", val); }; print(100); Console.WriteLine("Done"); Console.ReadLine(); }
private static void DoOperation(List <Token> tokens, ref int pos, OperationDel del, string content) { tokens[pos] = del(tokens, pos); for (int i = 1; i <= operandsNumList[content]; i++) { tokens.RemoveAt(pos - i); } pos -= operandsNumList[content]; }
static void Operate(OperationDel operationDel, string url, StatusDel success, StatusDel failure) { string data = operationDel(url); if (data != null && data != string.Empty) { success(data); } else { failure("could not get data...check the url"); } }
public static void processAndShow(OperationDel del, int val) { del(ref val); Console.Write(val + " "); }