ActionmessageDelegate = (message) => { Console.WriteLine($"Received message: {message}"); }; messageDelegate("Hello world!");
ListThis example creates an Action delegate that takes an integer parameter and writes it to the console. It then uses the ForEach method of a List to invoke the delegate for each element in the list. Package library: System.Linq.dllnumbers = new List {1, 2, 3, 4, 5}; Action printNumber = (number) => { Console.WriteLine(number); }; numbers.ForEach(printNumber);