A repository of exercises to support the training.
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.
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:
MotorAction
that takes an integer
representing motor speed.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:
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:
class RobotSensors
{
public void CheckTemperature() { /* Temperature logic */ }
public void CheckProximity() { /* Proximity logic */ }
public void CheckBattery() { /* Battery logic */ }
/* and so on */
}
Expected Outcome:
Explore how anonymous methods can simplify your code when handling custom actions in the robot control system.
Instructions:
MotorAction alert = delegate(int temp)
{
if (temp > 100)
Console.WriteLine("Overheating! Shutting down motor.");
};
Expected Outcome:
Explore the difference between instance and static methods by applying them to robot control tasks.
Instructions:
public delegate void ArmControl();
class RobotArms
{
public void MoveArms() { /* Instance method */ }
public static void ResetArms() { /* Static method */ }
}
Expected Outcome:
Enjoy controlling your robotic systems with the power of delegates!