Exercise: Flexible Logger

Implement a multicast delegate using Action<string> to distribute package delivery logs.

Problem statement

You are building a logistics system that tracks package deliveries. When a package arrives at a facility, the system needs to record the event. Currently, the system prints only to the console. You must upgrade the system into a complete utility that processes a list of tracking numbers and simultaneously prints a system log and simulates sending an SMS notification to the customer using a single delegate invocation.

Task requirements

  • Create a list or array of tracking numbers to process.

  • Define a logging mechanism that accepts a message string.

  • Configure the logger to print the message to the console with a [System] prefix.

  • Configure the same logger to print the message to the console with an [SMS] prefix.

  • Loop through the tracking numbers and trigger the logger once per package.

Constraints

  • You must use the built-in Action<string> delegate type.

  • You must use lambda expressions to define the logging behaviors.

  • You must combine the behaviors using multicast delegate syntax (+=) so a single invocation executes both actions.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Remember that Action<T> takes a parameter of type T and returns void.

  • You can assign the first lambda using the = operator and append the second using the += operator.

  • When you invoke a multicast delegate, you call it exactly like a standard method, passing the required arguments inside the parentheses.

Exercise: Flexible Logger

Implement a multicast delegate using Action<string> to distribute package delivery logs.

Problem statement

You are building a logistics system that tracks package deliveries. When a package arrives at a facility, the system needs to record the event. Currently, the system prints only to the console. You must upgrade the system into a complete utility that processes a list of tracking numbers and simultaneously prints a system log and simulates sending an SMS notification to the customer using a single delegate invocation.

Task requirements

  • Create a list or array of tracking numbers to process.

  • Define a logging mechanism that accepts a message string.

  • Configure the logger to print the message to the console with a [System] prefix.

  • Configure the same logger to print the message to the console with an [SMS] prefix.

  • Loop through the tracking numbers and trigger the logger once per package.

Constraints

  • You must use the built-in Action<string> delegate type.

  • You must use lambda expressions to define the logging behaviors.

  • You must combine the behaviors using multicast delegate syntax (+=) so a single invocation executes both actions.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Remember that Action<T> takes a parameter of type T and returns void.

  • You can assign the first lambda using the = operator and append the second using the += operator.

  • When you invoke a multicast delegate, you call it exactly like a standard method, passing the required arguments inside the parentheses.

C# 14.0
string[] trackingNumbers = { "PKG-1001", "PKG-1002", "PKG-1003" };
// 1. Declare an Action<string> delegate variable named 'deliveryLogger'
// 2. Assign a lambda expression that prints: "[System] Package {message} arrived."
// 3. Add a second lambda expression that prints: "[SMS] Alert for {message}."
Console.WriteLine("Starting batch processing...");
foreach (var trackingNumber in trackingNumbers)
{
// 4. Invoke the delegate with the current tracking number
}
Console.WriteLine("Batch processing complete.");