Exercise: IoT Sensor Reading
Problem statement
In IoT (Internet of Things) systems, sensors occasionally fail to transmit data due to network drops or hardware glitches. The objective is to build a data processor for a greenhouse. If a temperature sensor fails to send a reading, the system must substitute a safe default temperature to keep the greenhouse climate control running smoothly.
Task requirements
Create a
SensorReadingstruct.Store the recorded temperature in Celsius, accounting for potentially missing values.
Implement a method that returns the recorded temperature if it exists, or a provided default temperature if the reading is missing.
Constraints
Use a
structfor the data model.Use a nullable value type (
double?) to represent the potentially missing temperature.Use the null-coalescing operator (
??) inside the method to determine the returned temperature.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
To make a value type nullable, append a
?to the type name (e.g.,double?).The null-coalescing operator (
??) evaluates the left operand first. If the left operand is null, it returns the right operand.Structs support parameterized constructors to easily initialize read-only properties.
Exercise: IoT Sensor Reading
Problem statement
In IoT (Internet of Things) systems, sensors occasionally fail to transmit data due to network drops or hardware glitches. The objective is to build a data processor for a greenhouse. If a temperature sensor fails to send a reading, the system must substitute a safe default temperature to keep the greenhouse climate control running smoothly.
Task requirements
Create a
SensorReadingstruct.Store the recorded temperature in Celsius, accounting for potentially missing values.
Implement a method that returns the recorded temperature if it exists, or a provided default temperature if the reading is missing.
Constraints
Use a
structfor the data model.Use a nullable value type (
double?) to represent the potentially missing temperature.Use the null-coalescing operator (
??) inside the method to determine the returned temperature.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
To make a value type nullable, append a
?to the type name (e.g.,double?).The null-coalescing operator (
??) evaluates the left operand first. If the left operand is null, it returns the right operand.Structs support parameterized constructors to easily initialize read-only properties.