Exercise: IoT Sensor Reading

Model an IoT sensor using a struct and nullable types to safely handle missing temperature data.

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 SensorReading struct.

  • 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 struct for 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

Model an IoT sensor using a struct and nullable types to safely handle missing temperature data.

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 SensorReading struct.

  • 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 struct for 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.

C# 14.0
SensorReading validReading = new SensorReading(22.5);
SensorReading missingReading = new SensorReading(null);
Console.WriteLine($"Valid reading: {validReading.GetEffectiveTemperature(20.0)}°C");
Console.WriteLine($"Missing reading: {missingReading.GetEffectiveTemperature(20.0)}°C");
// TODO: Define a public struct named SensorReading
// TODO: Inside the struct, add a public property for the temperature (double?)
// TODO: Create a constructor to initialize the temperature property
// TODO: Create a method GetEffectiveTemperature(double defaultTemp) that uses ?? to return the value