public delegate void MyDelegate(); class MyClass { public event MyDelegate MyEvent; public void CallMyEvent() { if (MyEvent != null) { // Get the invocation list of MyEvent and loop over it foreach (MyDelegate del in MyEvent.GetInvocationList()) { del(); // Invoke each delegate } } } }
public delegate int MathOperation(int x, int y); class Program { static void Main(string[] args) { MathOperation operation = Add; operation += Subtract; operation += Multiply; operation += Divide; // Get the invocation list of the delegate and print each one's name foreach (MathOperation op in operation.GetInvocationList()) { Console.WriteLine(op.Method.Name); } Console.ReadLine(); } static int Add(int x, int y) { return x + y; } static int Subtract(int x, int y) { return x - y; } static int Multiply(int x, int y) { return x * y; } static int Divide(int x, int y) { if (y == 0) throw new ArgumentException("y cannot be zero."); return x / y; } }In this example, a delegate `MathOperation` is declared, which represents mathematical operations that take two `int` parameters and return an `int` result. Four methods are declared that match this signature: `Add`, `Subtract`, `Multiply`, and `Divide`. An instance of the delegate is created, and each of the four methods is added to its invocation list. The `GetInvocationList` method is then called on the delegate, which returns an array of the individual delegate instances in the multicast delegate. The name of each method in the list is printed to the console. These examples use the `System` namespace, which is part of the .NET Framework Class Library. Therefore, the package library is the .NET Framework Class Library.