Mastering The C# Programming Language

A repository of exercises to support the training.

View the Project on GitHub dvwl/mastering-c-sharp

Module 03a: Mastering Delegates: Controlling a Robotics System

In this module, you'll explore the power of delegates by creating a simple robotics control system. You'll learn how to use delegates to handle different actions, such as sensor inputs, motor control, and system alerts, with flexibility and modularity.

Exercise 1: Basic Delegate for Motor Control

Your first task is to create a basic delegate that simulates controlling a robot’s motor. You'll use delegates to define actions like starting, stopping, and adjusting motor speed.

Instructions:

  1. Define a delegate MotorAction that takes an integer representing motor speed.
  2. Create methods to start the motor, stop the motor, and adjust the speed.
  3. Use the delegate to call these methods based on different scenarios.

Class Example:

public delegate void MotorAction(int speed);

class MotorController
{
    public void StartMotor(int speed) { /* Start motor logic */ }
    public void StopMotor(int speed) { /* Stop motor logic */ }
    public void AdjustSpeed(int speed) { /* Adjust speed logic */ }
}

Expected Outcome:

Exercise 2: Multicast Delegate for Sensor Monitoring

Now, extend your knowledge by creating a multicast delegate that triggers multiple sensor checks in the robot (e.g., temperature, proximity, or battery levels).

Instructions:

  1. Create a delegate SensorCheck that doesn’t take any parameters.
  2. Create methods for different sensors: checking temperature, proximity, and battery.
  3. Use a multicast delegate to call all the sensor checks at once.
class RobotSensors
{
    public void CheckTemperature() { /* Temperature logic */ }
    public void CheckProximity() { /* Proximity logic */ }
    public void CheckBattery() { /* Battery logic */ }
	/* and so on */
}

Expected Outcome:

Exercise 3: Anonymous Methods for Custom Actions

Explore how anonymous methods can simplify your code when handling custom actions in the robot control system.

Instructions:

  1. Use an anonymous method within a delegate to create a quick system alert (e.g., overheating alert).
  2. Call the anonymous method whenever a condition (e.g., high temperature) is met.
MotorAction alert = delegate(int temp)
{
    if (temp > 100)
        Console.WriteLine("Overheating! Shutting down motor.");
};

Expected Outcome:

Exercise 4: Instance and Static Methods with Delegates

Explore the difference between instance and static methods by applying them to robot control tasks.

Instructions:

  1. Create both instance and static methods for controlling the robot's arms.
  2. Assign them to the delegate ArmControl, and test how both types of methods work.
public delegate void ArmControl();
class RobotArms
{
    public void MoveArms() { /* Instance method */ }
    public static void ResetArms() { /* Static method */ }
}

Expected Outcome:

Key Takeaways

Enjoy controlling your robotic systems with the power of delegates!