Addressed nullabilty in Chapter 14.
MODIFY
src/Chapter14/Listing14.02.DefiningTheEventPublsherThermostat.cs
src/Chapter14/Listing14.02.DefiningTheEventPublsherThermostat.cs
{
// Using C# 3.0 or later syntax.
// Define the event publisher (initially without the sender)
- public Action<float> OnTemperatureChange { get; set; }
+ public Action<float>? OnTemperatureChange { get; set; }
public float CurrentTemperature
{

MODIFY
src/Chapter14/Listing14.04.InvokingADelegateWithoutCheckingForNull.cs
src/Chapter14/Listing14.04.InvokingADelegateWithoutCheckingForNull.cs
public class Thermostat
{
// Define the event publisher
- public Action<float> OnTemperatureChange { get; set; }
+ public Action<float>? OnTemperatureChange { get; set; }
public float CurrentTemperature
{
{
_CurrentTemperature = value;
- // INCOMPLETE: Check for null needed
// Call subscribers
+ // Justification: Incomplete, check for null needed.
+ #pragma warning disable CS8602 // Dereference of a possibly null reference.
OnTemperatureChange(value);
+ #pragma warning restore CS8602 // Dereference of a possibly null reference.
}
}
}

MODIFY
src/Chapter14/Listing14.05.InvokingADelegate.cs
src/Chapter14/Listing14.05.InvokingADelegate.cs
public class Thermostat
{
// Define the event publisher
- public Action<float> OnTemperatureChange { get; set; }
+ public Action<float>? OnTemperatureChange { get; set; }
public float CurrentTemperature
{

MODIFY
src/Chapter14/Listing14.06.InvokingADelegatePre6.cs
src/Chapter14/Listing14.06.InvokingADelegatePre6.cs
public class Thermostat
{
// Define the event publisher
- public Action<float> OnTemperatureChange { get; set; }
+ public Action<float>? OnTemperatureChange { get; set; }
public float CurrentTemperature
{
// If there are any subscribers,
// notify them of changes in
// temperature
- Action<float> localOnChange =
+ #pragma warning disable IDE1005 // Delegate invocation can be simplified.
+ Action<float>? localOnChange =
OnTemperatureChange;
if(localOnChange != null)
{
// Call subscribers
localOnChange(value);
}
+ #pragma warning restore IDE1005 // Delegate invocation can be simplified.
}
}
}

MODIFY
src/Chapter14/Listing14.07.UsingthePlusEqualsAndMinusEqualsDelegateOperators.cs
src/Chapter14/Listing14.07.UsingthePlusEqualsAndMinusEqualsDelegateOperators.cs
Action<float> delegate1;
Action<float> delegate2;
- Action<float> delegate3;
+ Action<float>? delegate3;
// use Constructor syntax for C# 1.0
delegate1 = heater.OnTemperatureChanged;
Console.WriteLine("Invoke only delegate2");
delegate3 -= delegate1;
- delegate3(30);
+ delegate3!(30);
}
}
}
\ No newline at end of file

MODIFY
src/Chapter14/Listing14.08.UsingThePlusAndMinusDelegateOperators.cs
src/Chapter14/Listing14.08.UsingThePlusAndMinusDelegateOperators.cs
delegate3(60);
Console.WriteLine("Uncombine delegates using - operator:");
- delegate3 = delegate3 - delegate2;
+ delegate3 = (delegate3 - delegate2)!;
delegate3(60);
}
}

MODIFY
src/Chapter14/Listing14.10.HandlingExceptionsFromSubscribers.cs
src/Chapter14/Listing14.10.HandlingExceptionsFromSubscribers.cs
{
// Using C# 3.0 or later syntax
// Define the event publisher
- public Action<float> OnTemperatureChange;
+ public Action<float>? OnTemperatureChange;
public float CurrentTemperature
{
if(value != CurrentTemperature)
{
_CurrentTemperature = value;
- Action<float> onTemperatureChange = OnTemperatureChange;
+ Action<float>? onTemperatureChange = OnTemperatureChange;
if (onTemperatureChange != null)
{
List<Exception> exceptionCollection =

MODIFY
src/Chapter14/Listing14.15.FiringTheEventNotification.cs
src/Chapter14/Listing14.15.FiringTheEventNotification.cs
NewTemperature = newTemperature;
}
- public float NewTemperature
- {
- get { return _newTemperature; }
- set { _newTemperature = value; }
- }
- private float _newTemperature;
+ public float NewTemperature { get; set; }
}
// Define the event publisher

MODIFY
src/Chapter14/Listing14.16.UsingCustomDelegateType.cs
src/Chapter14/Listing14.16.UsingCustomDelegateType.cs
public delegate void TemperatureChangeHandler(
object sender, TemperatureArgs newTemperature);
- public event TemperatureChangeHandler
+ public event TemperatureChangeHandler?
OnTemperatureChange;
public float CurrentTemperature
// If there are any subscribers
// then notify them of changes in
// temperature
- if(OnTemperatureChange != null)
- {
// Call subscribers
- OnTemperatureChange(
+ OnTemperatureChange?.Invoke(
this, new TemperatureArgs(value));
}
}
}
- }
private float _CurrentTemperature;
}
}
\ No newline at end of file

MODIFY
src/Chapter14/Listing14.19.CustomAddAndRemoveHandlers.cs
src/Chapter14/Listing14.19.CustomAddAndRemoveHandlers.cs
NewTemperature = newTemperature;
}
- public float NewTemperature
- {
- get { return _newTemperature; }
- set { _newTemperature = value; }
- }
- private float _newTemperature;
+ public float NewTemperature { get; set; }
}
// Define the delegate data type
}
remove
{
- _OnTemperatureChange = (TemperatureChangeHandler)System.Delegate.Remove(_OnTemperatureChange, value);
+ _OnTemperatureChange =
+ (TemperatureChangeHandler?)System.Delegate.Remove(_OnTemperatureChange, value);
}
}
- protected TemperatureChangeHandler _OnTemperatureChange;
+ protected TemperatureChangeHandler? _OnTemperatureChange;
public float CurrentTemperature
{

Addressed nullabilty in Chapter 14.