Exercise: Security System Alert

Encapsulate a delegate using the event keyword and subscribe multiple lambda expressions to handle alerts.

Problem statement

You are developing software for a smart home security hub. The SecuritySystem class detects motion and triggers a public delegate. A junior developer left the delegate field public, meaning external classes can maliciously clear the alert list or trigger fake alarms. You must secure this class and configure a complete utility to test multiple sensor zones.

Task requirements

  • Secure the SecuritySystem class so external code can only subscribe or unsubscribe from the motion alert.

  • In the top-level execution code, subscribe to the alert to print "Turning on floodlights for {zone}...".

  • Subscribe a second action to print "Sounding main alarm for {zone}...".

  • Trigger the detection mechanism by calling DetectMotion for different zones like “Backyard” and “Garage”.

Constraints

  • You must use the event keyword to encapsulate the SensorTripped delegate inside the SecuritySystem class.

  • You must use lambda expressions to subscribe to the event.

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

Get hints

  • Adding a single keyword to the SensorTripped declaration inside SecuritySystem will lock down external access.

  • Since the delegate takes a string parameter, your lambda expressions must accept one parameter representing the zone.

  • You cannot invoke an event directly from the top-level code. You must rely on the DetectMotion method to raise the event internally.

Exercise: Security System Alert

Encapsulate a delegate using the event keyword and subscribe multiple lambda expressions to handle alerts.

Problem statement

You are developing software for a smart home security hub. The SecuritySystem class detects motion and triggers a public delegate. A junior developer left the delegate field public, meaning external classes can maliciously clear the alert list or trigger fake alarms. You must secure this class and configure a complete utility to test multiple sensor zones.

Task requirements

  • Secure the SecuritySystem class so external code can only subscribe or unsubscribe from the motion alert.

  • In the top-level execution code, subscribe to the alert to print "Turning on floodlights for {zone}...".

  • Subscribe a second action to print "Sounding main alarm for {zone}...".

  • Trigger the detection mechanism by calling DetectMotion for different zones like “Backyard” and “Garage”.

Constraints

  • You must use the event keyword to encapsulate the SensorTripped delegate inside the SecuritySystem class.

  • You must use lambda expressions to subscribe to the event.

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

Get hints

  • Adding a single keyword to the SensorTripped declaration inside SecuritySystem will lock down external access.

  • Since the delegate takes a string parameter, your lambda expressions must accept one parameter representing the zone.

  • You cannot invoke an event directly from the top-level code. You must rely on the DetectMotion method to raise the event internally.

C# 14.0
var securityHub = new SecuritySystem();
// 1. Subscribe a lambda to turn on floodlights
// 2. Subscribe a lambda to sound the main alarm
Console.WriteLine("System Armed.");
// 3. Call securityHub.DetectMotion("Backyard")
// 4. Call securityHub.DetectMotion("Garage")
// --- Class Definitions ---
public delegate void SensorTrippedHandler(string zone);
public class SecuritySystem
{
// Fix this field to properly encapsulate the delegate
public SensorTrippedHandler? SensorTripped;
public void DetectMotion(string zone)
{
Console.WriteLine($"\n> Motion detected in zone: {zone}");
SensorTripped?.Invoke(zone);
}
}