1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
using System;
public class Blah {
public delegate int MyDelegate (int i, int j);
public int Foo (int i, int j)
{
return i+j;
}
public static int Test1 ()
{
Blah f = new Blah ();
MyDelegate del = new MyDelegate (f.Foo);
MyDelegate another = new MyDelegate (del);
int number = del (2, 3);
int i = another (4, 6);
Console.WriteLine ("Delegate invocation of one returned : " + number);
Console.WriteLine ("Delegate invocation of the other returned : " + i);
if (number == 5 && i == 10)
return 0;
else
return 1;
}
public delegate int List (params int [] args);
public static int Adder (params int [] args)
{
int total = 0;
foreach (int i in args)
total += i;
return total;
}
public static int Test2 ()
{
List my_adder = new List (Adder);
if (my_adder (1, 2, 3) != 6)
return 2;
return 0;
}
public static int Main ()
{
int v;
v = Test1 ();
if (v != 0)
return v;
v = Test2 ();
if (v != 0)
return v;
Console.WriteLine ("All tests pass");
return 0;
}
}
|