2 // delegate declaration
3 delegate void MyDelegate(int i
);
7 public static void Main()
9 TakesADelegate(new MyDelegate(DelegateFunction
));
12 public static void TakesADelegate(MyDelegate SomeFunction
)
17 public static void DelegateFunction(int i
)
19 System
.Console
.WriteLine("Called by delegate with number: {0}.", i
);
23 // keyword_delegate2.cs
24 // Calling both static and instance methods from delegates
27 // delegate declaration
28 delegate void MyDelegate();
32 public void InstanceMethod()
34 Console
.WriteLine("A message from the instance method.");
37 static public void StaticMethod()
39 Console
.WriteLine("A message from the static method.");
43 public class MainClass
45 static public void Main()
47 MyClass p
= new MyClass();
49 // Map the delegate to the instance method:
50 MyDelegate d
= new MyDelegate(p
.InstanceMethod
);
53 // Map to the static method:
54 d
= new MyDelegate(MyClass
.StaticMethod
);