Addressed nullabilty in Chapter 20.
MODIFY
src/Chapter20/Listing20.07.FiringEventNotification.cs
src/Chapter20/Listing20.07.FiringEventNotification.cs
class Program
{
- private static TemperatureChangedHandler OnTemperatureChanged;
+ private static readonly TemperatureChangedHandler OnTemperatureChanged = delegate { };
public void Main()
{
// Not thread-safe
- if(OnTemperatureChanged != null)
+ if(OnTemperatureChanged != null) // Warning ignored to demonstrate problem.
{
// Call subscribers
OnTemperatureChanged(
}
}
- public object value { get; set; }
+ // Justification: Lowercase to simulate the value keyword form a stetter.
+ #pragma warning disable IDE1006 // Naming Styles
+ public object? value { get; set; }
+ #pragma warning restore IDE1006 // Naming Styles
}
class TemperatureEventArgs
{
- public TemperatureEventArgs(object value)
+ public TemperatureEventArgs(object? _)
{
// ...
}

MODIFY
src/Chapter20/Listing20.08.ThreadSafeEventNotification.cs
src/Chapter20/Listing20.08.ThreadSafeEventNotification.cs
class Program
{
- private static TemperatureChangedHandler OnTemperatureChanged;
+ private static TemperatureChangedHandler OnTemperatureChanged = delegate { };
public void Main()
{
#endif
}
- public object value { get; set; }
+ // Justification: Lowercase to simulate the value keyword form a stetter.
+ #pragma warning disable IDE1006 // Naming Styles
+ public object? value { get; set; }
+ #pragma warning restore IDE1006 // Naming Styles
}
class TemperatureEventArgs
{
- public TemperatureEventArgs(object value)
+ public TemperatureEventArgs(object? _)
{
// ...
}

MODIFY
src/Chapter20/Listing20.09.CreatingASingleInstanceApplication.cs
src/Chapter20/Listing20.09.CreatingASingleInstanceApplication.cs
{
public static void Main()
{
- // Indicates whether this is the first
- // application instance.
- bool firstApplicationInstance;
-
// Obtain the mutex name from the full
// assembly name.
string mutexName =
- Assembly.GetEntryAssembly().FullName;
+ Assembly.GetEntryAssembly()!.FullName!;
- using(Mutex mutex = new Mutex(false, mutexName,
- out firstApplicationInstance))
- {
+ // firstApplicationInstance indicates whether this is the first
+ // application instance.
+ using Mutex mutex = new Mutex(false, mutexName,
+ out bool firstApplicationInstance);
if (!firstApplicationInstance)
{
}
}
}
-}

MODIFY
src/Chapter20/Listing20.10.WaitingForManualResetEventSlim.cs
src/Chapter20/Listing20.10.WaitingForManualResetEventSlim.cs
public class Program
{
+ // Justification: Initialized at the start of Main.
+ #pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
static ManualResetEventSlim MainSignaledResetEvent;
static ManualResetEventSlim DoWorkSignaledResetEvent;
+ #pragma warning restore CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
public static void DoWork()
{

MODIFY
src/Chapter20/PiCalculation.cs
src/Chapter20/PiCalculation.cs
@@ -14,10 +14,10 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter20
CalculateAsync(digits, null);
}
public void CalculateAsync(
- int digits, object userState)
+ int digits, object? userState)
{
CalculateAsync(
- digits, default(CancellationToken), userState);
+ digits, default, userState);
}
public void CalculateAsync<TState>(
int digits,
@@ -29,7 +29,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter20
.ContinueWith<string>(
continueTask =>
{
- CalculateCompleted(typeof(PiCalculator),
+ CalculateCompleted?.Invoke(typeof(PiCalculator),
new CalculateCompletedEventArgs(
continueTask.Result,
continueTask.Exception,
@@ -47,9 +47,9 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter20
{
public CalculateCompletedEventArgs(
string value,
- Exception error,
+ Exception? error,
bool cancelled,
- object userState)
+ object? userState)
: base(
error, cancelled, userState)
{

Addressed nullabilty in Chapter 20.