Addressed nullabilty in Chapter 18.
MODIFY
src/Chapter18/Listing18.03.DynamicallyInvokingAMember.cs
src/Chapter18/Listing18.03.DynamicallyInvokingAMember.cs
@@ -8,10 +8,9 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_03
{
public static void Main(string[] args)
{
- string errorMessage;
CommandLineInfo commandLine = new CommandLineInfo();
if(!CommandLineHandler.TryParse(
- args, commandLine, out errorMessage))
+ args, commandLine, out string? errorMessage))
{
Console.WriteLine(errorMessage);
DisplayHelp();
@@ -50,7 +49,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_03
{
public bool Help { get; set; }
- public string Out { get; set; }
+ public string? Out { get; set; }
public ProcessPriorityClass Priority { get; set; }
= ProcessPriorityClass.Normal;
@@ -62,15 +61,14 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_03
{
public static void Parse(string[] args, object commandLine)
{
- string errorMessage;
- if(!TryParse(args, commandLine, out errorMessage))
+ if(!TryParse(args, commandLine, out string? errorMessage))
{
throw new Exception(errorMessage);
}
}
public static bool TryParse(string[] args, object commandLine,
- out string errorMessage)
+ out string? errorMessage)
{
bool success = false;
errorMessage = null;
@@ -84,7 +82,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_03
// Remove the slash|dash
option = optionParts[0].Remove(0, 1);
- PropertyInfo property =
+ PropertyInfo? property =
commandLine.GetType().GetProperty(option,
BindingFlags.IgnoreCase |
BindingFlags.Instance |
@@ -131,10 +129,8 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_03
success = false;
errorMessage =
$@"Data type '{
- property.PropertyType.ToString()
- }' on {
- commandLine.GetType().ToString()
- } is not supported.";
+ property.PropertyType }' on {
+ commandLine.GetType() } is not supported.";
}
}
else

MODIFY
src/Chapter18/Listing18.07.LeveragingNameofWithINotifyPropertyChanged.cs
src/Chapter18/Listing18.07.LeveragingNameofWithINotifyPropertyChanged.cs
@@ -5,12 +5,13 @@ using System.ComponentModel;
public class Person : INotifyPropertyChanged
{
- public event PropertyChangedEventHandler PropertyChanged;
+ public event PropertyChangedEventHandler PropertyChanged = delegate { };
+
public Person(string name)
{
Name = name;
}
- private string _Name;
+ private string _Name=string.Empty;
public string Name
{
get { return _Name; }

MODIFY
src/Chapter18/Listing18.08.DecoratingAPropertyWithAnAttribute.cs
src/Chapter18/Listing18.08.DecoratingAPropertyWithAnAttribute.cs
@@ -8,7 +8,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_08
public bool Help { get; set; }
[CommandLineSwitchRequired]
- public string Out { get; set; }
+ public string? Out { get; set; }
public System.Diagnostics.ProcessPriorityClass Priority
{ get; set; } =
@@ -24,7 +24,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_08
internal class CommandLineSwitchAliasAttribute : Attribute
{
- public CommandLineSwitchAliasAttribute(string s)
+ public CommandLineSwitchAliasAttribute(string _)
{
//not implimented
}

MODIFY
src/Chapter18/Listing18.09.DecoratingAPropertyWithMultipleAttributes.cs
src/Chapter18/Listing18.09.DecoratingAPropertyWithMultipleAttributes.cs
@@ -12,7 +12,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_09
//[CommandLineSwitchAlias("FileName")]
[CommandLineSwitchRequired,
CommandLineSwitchAlias("FileName")]
- public string Out { get; set; }
+ public string? Out { get; set; }
public System.Diagnostics.ProcessPriorityClass Priority
{ get; set; } =
@@ -26,7 +26,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_09
internal class CommandLineSwitchAliasAttribute : Attribute
{
- public CommandLineSwitchAliasAttribute(string s)
+ public CommandLineSwitchAliasAttribute(string _)
{
//not implimented
}

MODIFY
src/Chapter18/Listing18.15.RetrievingASpecificAttributeAndCheckingItsInitialization.cs
src/Chapter18/Listing18.15.RetrievingASpecificAttributeAndCheckingItsInitialization.cs
@@ -10,7 +10,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_15
public static void Main()
{
PropertyInfo property =
- typeof(CommandLineInfo).GetProperty("Help");
+ typeof(CommandLineInfo).GetProperty("Help")!;
CommandLineSwitchAliasAttribute attribute =
(CommandLineSwitchAliasAttribute)
property.GetCustomAttributes(

MODIFY
src/Chapter18/Listing18.16.RetrievingCustomAttributeInstances.cs
src/Chapter18/Listing18.16.RetrievingCustomAttributeInstances.cs
@@ -16,7 +16,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_16
public static Dictionary<string, PropertyInfo> GetSwitches(
object commandLine)
{
- PropertyInfo[] properties = null;
+ PropertyInfo[] properties;
Dictionary<string, PropertyInfo> options =
new Dictionary<string, PropertyInfo>();

MODIFY
src/Chapter18/Listing18.17.UpdatingCommandLineHandler.TryParseToHandleAliases.cs
src/Chapter18/Listing18.17.UpdatingCommandLineHandler.TryParseToHandleAliases.cs
@@ -10,7 +10,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_17
// ...
public static bool TryParse(
string[] args, object commandLine,
- out string errorMessage)
+ out string? errorMessage)
{
bool success = false;
errorMessage = null;
@@ -21,7 +21,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_17
foreach(string arg in args)
{
- PropertyInfo property;
string option;
if(arg[0] == '/' || arg[0] == '-')
{
@@ -29,7 +28,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_17
new char[] { ':' }, 2);
option = optionParts[0].Remove(0, 1).ToLower();
- if(options.TryGetValue(option, out property))
+ if(options.TryGetValue(option, out PropertyInfo? property))
{
success = SetOption(
commandLine, property,
@@ -48,7 +47,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_17
private static bool SetOption(
object commandLine, PropertyInfo property,
- string[] optionParts, ref string errorMessage)
+ string[] optionParts, ref string? errorMessage)
{
bool success;
@@ -96,9 +95,13 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_17
return success;
}
- private static bool TryParseEnumSwitch(object commandLine, string[] optionParts, PropertyInfo property, ref string errorMessage)
+// Justification: Not fully implemented.
+#pragma warning disable IDE0060 // Remove unused parameter
+ private static bool TryParseEnumSwitch(
+ object commandLine, string[] optionParts, PropertyInfo property, ref string? errorMessage)
{
throw new NotImplementedException();
}
+#pragma warning restore IDE0060 // Remove unused parameter
}
}

MODIFY
src/Chapter18/Listing18.32.ImplementingACustomDynamicObject.cs
src/Chapter18/Listing18.32.ImplementingACustomDynamicObject.cs
@@ -19,7 +19,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_32
}
public override bool TryGetMember(
- GetMemberBinder binder, out object result)
+ GetMemberBinder binder, out object? result)
{
bool success = false;
result = null;

MODIFY
src/Shared/DoWorkEventArgs.cs
src/Shared/DoWorkEventArgs.cs
@@ -5,9 +5,9 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Shared
{
public class DoWorkEventArgs : EventArgs
{
- public DoWorkEventArgs(object argument) { this.Argument = argument; }
+ public DoWorkEventArgs(object argument) { Argument = argument; }
public object Argument { get; }
public bool Cancel { get; set; }
- public object Result { get; set; }
+ public object? Result { get; set; }
}
}

MODIFY
src/Shared/PiCalculator.cs
src/Shared/PiCalculator.cs
@@ -12,7 +12,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Shared
return Calculate();
}
- public static string Calculate(int digits = 100)
+ public static string Calculate(int digits = Digits)
{
return Calculate(digits, 0);
}
@@ -22,7 +22,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Shared
DoWorkEventArgs eventArgs = new DoWorkEventArgs(digits);
CalculatePi(typeof(PiCalculator), eventArgs, startingAt);
- return (string)eventArgs.Result;
+ return (string)eventArgs.Result!;
}
private static void CalculatePi(

Addressed nullabilty in Chapter 18.