using System;
public delegate void DelegateClass();
public class Starter {
public static void Main() {
DelegateClass del = (DelegateClass)
DelegateClass.Combine(new DelegateClass[] { MethodA, MethodB, MethodA, MethodB });
del();
foreach (DelegateClass item in
del.GetInvocationList()) {
Console.WriteLine(item.Method.Name + " in invocation list.");
}
}
public static void MethodA() {
Console.WriteLine("MethodA...");
}
public static void MethodB() {
Console.WriteLine("MethodB...");
}
}
Language Basics
To remove delegates from a multicast delegate, use the Remove method, the minus operator (-), or the -= assignment operator.
using System;
public delegate void DelegateClass();
public class Starter {
public static void Main(){
DelegateClass del=MethodA;
del+=MethodB;
del+=MethodC;
del=del-MethodB;
del();
}
public static void MethodA() {
Console.WriteLine("MethodA...");
}
public static void MethodB() {
Console.WriteLine("MethodB...");
}
public static void MethodC() {
Console.WriteLine("MethodC...");
}
}
Arrays of Delegates
using System;
public delegate void Task();
public class Starter {
public static void Main() {
Task[] tasks = { MethodA, MethodB, MethodC };
string resp;
tasks[0]();
tasks[1]();
tasks[2]();
}
public static void MethodA() {
Console.WriteLine("Doing TaskA");
}
public static void MethodB() {
Console.WriteLine("Doing TaskB");
}
public static void MethodC() {
Console.WriteLine("Doing TaskC");
}
}
Delegates:Multicasting
using System;
public class DelegatesMulticasting
{
delegate void ProcessHandler(string message);
static public void Process(string message)
{
Console.WriteLine("Test.Process("{0}")", message);
}
public static void Main()
{
User user = new User("George");
ProcessHandler ph = new ProcessHandler(user.Process);
ph = (ProcessHandler) Delegate.Combine(ph, new ProcessHandler(Process));
ph("Wake Up!");
}
}
public class User
{
string name;
public User(string name)
{
this.name = name;
}
public void Process(string message)
{
Console.WriteLine("{0}: {1}", name, message);
}
}
Delegates to Instance Members
using System;
public class DelegatestoInstanceMembers
{
delegate void ProcessHandler(string message);
public static void Main()
{
User aUser = new User("George");
ProcessHandler ph = new ProcessHandler(aUser.Process);
ph("Wake Up!");
}
}
public class User
{
string name;
public User(string name)
{
this.name = name;
}
public void Process(string message)
{
Console.WriteLine("{0}: {1}", name, message);
}
}
Delegates:Using Delegates
/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson
Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
// 22 – DelegatesUsing Delegates
// copyright 2000 Eric Gunnerson
using System;
public class DelegatesUsingDelegates
{
public static void Main()
{
Container employees = new Container();
// create and add some employees here
// create delegate to sort on names, and do the sort
Container.CompareItemsCallback sortByName =
new Container.CompareItemsCallback(Employee.CompareName);
employees.Sort(sortByName);
// employees is now sorted by name
}
}
public class Container
{
public delegate int CompareItemsCallback(object obj1, object obj2);
public void Sort(CompareItemsCallback compare)
{
// not a real sort, just shows what the
// inner loop code might do
int x = 0;
int y = 1;
object item1 = arr[x];
object item2 = arr[y];
int order = compare(item1, item2);
}
object[] arr = new object[1]; // items in the collection
}
public class Employee
{
Employee(string name, int id)
{
this.name = name;
this.id = id;
}
public static int CompareName(object obj1, object obj2)
{
Employee emp1 = (Employee) obj1;
Employee emp2 = (Employee) obj2;
return(String.Compare(emp1.name, emp2.name));
}
public static int CompareId(object obj1, object obj2)
{
Employee emp1 = (Employee) obj1;
Employee emp2 = (Employee) obj2;
if (emp1.id > emp2.id)
return(1);
if (emp1.id < emp2.id)
return(-1);
else
return(0);
}
string name;
int id;
}
[/csharp]
The minimum implementation of a delegate
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// Delegate.cs - Demonstrates the minimum implementation of a delegate
// Compile this program with the following command line:
// C:>csc Delegate.cs
//
namespace nsDelegate
{
using System;
public class Delegate21
{
// Declare the delegate type
public delegate double MathHandler (double val);
// Declare the variable that will hold the delegate
public MathHandler DoMath;
static public void Main ()
{
double val = 31.2;
double result;
Delegate21 main = new Delegate21();
// Create the first delegate
main.DoMath = new Delegate21.MathHandler (main.Square);
// Call the function through the delegate
result = main.DoMath (val);
Console.WriteLine ("The square of {0,0:F1} is {1,0:F3}",
val, result);
// Create the second delegate
main.DoMath = new Delegate21.MathHandler (main.SquareRoot);
// Call the function through the delegate
result = main.DoMath (val);
Console.WriteLine ("The square root of {0,0:F1} is {1,0:F3}",
val, result);
}
public double Square (double val)
{
return (val * val);
}
public double SquareRoot (double val)
{
return (Math.Sqrt (val));
}
}
}