void UseDelegatesAgain() { OtherDelegate myDelegate = new OtherDelegate(ThirdDelegate); int added = myDelegate(1, 2); Debug.Log("added: " + added); // "added: 3" myDelegate = FourthDelegate; int subtracted = myDelegate(5, 6); Debug.Log("subtracted: " + subtracted); // "subtracted: -1" }
public static void Main() { SomeDelegate d1 = Method; d1.BeginInvoke(null, null); OtherDelegate d2 = Method; d2.EndInvoke(null); StrippedDelegate d3 = Method; d3.DynamicInvoke(null); }
private static void Main(string[] args) { Print sth; int userValue = Convert.ToInt32(Console.ReadLine()); if (userValue % 2 == 0) { sth = Printer.PrintEven; } else { sth = Printer.PrintOdd; } sth(); // - - - - - - - - - - Console.WriteLine(); MathOperations math = new MathOperations(); Operation del = math.Sum; Console.WriteLine(del(3, 9)); del = math.Multiply; Console.WriteLine(del(3, 9)); // - - - - - - - - - - Console.WriteLine(); MyDelegate myDel = ManyMethods.ReturnAsSame; myDel += ManyMethods.Squaring; myDel += ManyMethods.MinusOne; myDel += ManyMethods.Squaring; myDel += ManyMethods.Squaring; Console.WriteLine("Return " + myDel(5)); Console.WriteLine(">>> New:"); myDel -= ManyMethods.Squaring; Console.WriteLine("Return " + myDel(5)); // - - - - - - - - - - Console.WriteLine(); OtherDelegate firstDelegate = OtherMethods.Greet; firstDelegate += OtherMethods.HowAreYou; OtherDelegate secondDelegate = OtherMethods.WhatsUp; secondDelegate += OtherMethods.Bye; OtherDelegate generalDelegate = firstDelegate + secondDelegate; generalDelegate("Mike"); // - - - - - - - - - - Console.WriteLine(); Invoke_Delegate(sth); // - - - - - - - - - - Console.WriteLine(); GDelegate <int> sqDel = Squaring; Console.WriteLine(sqDel(5)); }