public delegate void HelloWorldDelegate(); static void Main(string[] args) { HelloWorldDelegate hello = () => Console.WriteLine("Hello World!"); hello.DynamicInvoke(); }
public delegate int MathOperationDelegate(int x, int y); static void Main(string[] args) { MathOperationDelegate add = (x, y) => x + y; MathOperationDelegate subtract = (x, y) => x - y; Console.WriteLine(add.DynamicInvoke(10, 5)); // Output: 15 Console.WriteLine(subtract.DynamicInvoke(10, 5)); // Output: 5 }In this example, we create a delegate named `MathOperationDelegate` that takes two integers as parameters and returns an integer. We then use the lambda expression to initialize two delegates, `add` and `subtract`, with methods that perform addition and subtraction. Finally, we invoke the delegates dynamically using the `DynamicInvoke` method. Package/Library: The System Delegate DynamicInvoke is a part of the .NET framework and is included in the `mscorlib` assembly.