static void Main2(string[] args) { MyMath m1 = new MyMath(); MyMathDelegate d1 = new MyMathDelegate(m1.Addition); Console.WriteLine(d1(10, 20)); }
public void MulticastDelegateReturnValueTest() { MyMathDelegate doubleMe = (int num) => { return(num * 2); }; MyMathDelegate powMe = (int num) => (int)Math.Pow(num, 2); MyMathDelegate myDoubleMe = doubleMe; myDoubleMe(4).Should().Be(8); MyMathDelegate myPowMe = powMe; myPowMe(4).Should().Be(16); MyMathDelegate myAccumulator = doubleMe; myAccumulator += powMe; myAccumulator(4).Should().Be(16); // not 64 MyMathDelegate myAccumulator2 = doubleMe; myAccumulator2 += doubleMe; myAccumulator2(4).Should().Be(8); // not 16 //acc += powMe; //acc(2).Should().Be(16); //acc -= doubleMe; //acc(10).Should().Be(100); }
static void Main3() { MyDelegate d1 = new MyDelegate(f1); MyMath m1 = new MyMath(); MyMathDelegate d2 = new MyMathDelegate(m1.Addition); d2 += m1.Subtraction; d2 += m1.Multiplication; //Multicast delegate Test(d1, d2); }
static void Main() { MyMath m1 = new MyMath(); MyMathDelegate d1 = new MyMathDelegate(m1.Addition); MyMathDelegate d2 = new MyMathDelegate(m1.Subtraction); MyMathDelegate d3 = new MyMathDelegate(m1.Multiplication); e2 += d1; e2 += d2; e2 += d3; Console.WriteLine(e2(10, 20)); }
static void Main() { MyMath m1 = new MyMath(); MyMathDelegate d1 = new MyMathDelegate(m1.Addition); MyMathDelegate d2 = new MyMathDelegate(m1.Subtraction); MyMathDelegate d3 = new MyMathDelegate(m1.Multiplication); e2 += d1; e2 += d2; e2 += d3; Console.WriteLine(e2(10, 20)); }
static void Main3() { MyDelegate d1 = new MyDelegate(f1); MyMath m1 = new MyMath(); MyMathDelegate d2 = new MyMathDelegate(m1.Addition); d2 += m1.Subtraction; d2 += m1.Multiplication; //Multicast delegate Test(d1, d2); }
public static void Example2() { MyMathDelegate mathDelegate = new MyMathDelegate(MathUtility.Add); int num1 = 10; int num2 = 20; Console.WriteLine("Add({0}, {1}) = {2}", num1, num2, mathDelegate(num1, num2)); // Assign the Subract method reference to the delegate object. mathDelegate = MathUtility.Subtract; Console.WriteLine("Subtract({0}, {1}) = {2}", num1, num2, mathDelegate(num1, num2)); // Assign the Multiply method reference to the delegate object. mathDelegate = MathUtility.Multiply; Console.WriteLine("Multiply({0}, {1}) = {2}", num1, num2, mathDelegate(num1, num2)); }
static void Test(MyDelegate x, MyMathDelegate y) { x(); Console.WriteLine(y(10, 20)); }
static void Main2(string[] args) { MyMath m1 = new MyMath(); MyMathDelegate d1 = new MyMathDelegate(m1.Addition); Console.WriteLine(d1(10, 20)); }
static void Test(MyDelegate x, MyMathDelegate y) { x(); Console.WriteLine(y(10, 20)); }