delegate void MyCallback(string message); class MyClass { public void DoSomething(MyCallback callback) { string message = "Hello, world!"; callback(message); } } class Program { static void Main(string[] args) { MyClass myClass = new MyClass(); MyCallback callback = new MyCallback(PrintMessage); myClass.DoSomething(callback); } static void PrintMessage(string message) { Console.WriteLine(message); } }
class MyClass { public void DoSomething(ActionIn this example, the `MyClass` class has a method `DoSomething` that takes an `Actioncallback) { string message = "Hello, world!"; callback(message); } } class Program { static void Main(string[] args) { MyClass myClass = new MyClass(); myClass.DoSomething(PrintMessage); } static void PrintMessage(string message) { Console.WriteLine(message); } }