Exercise: Flexible Logger
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 typeTand returnsvoid.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
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 typeTand returnsvoid.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.