Updated Listing 4.38 to have full implementation; Disable various warnings caused by sample code that was not fully flushed out; Minor updates to #pragma example.
ADD
src/Chapter04.Tests/Listing04.38.Tests.cs
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_38.Tests
+{
+ [TestClass]
+ public class ProgramTests
+ {
+ [TestMethod]
+ public void ChangeTemperatureWithoutConditionalOperature()
+ {
+ bool eventFired = false;
+ Thermostat thermostat = new Thermostat();
+ thermostat.PropertyChanged += (object? sender, System.EventArgs e) =>
+ {
+ eventFired = true;
+ };
+ thermostat.Temperature = 42;
+ Assert.IsTrue(eventFired);
+ }
+
+ public void ChangeHumidityWithConditionalOperature()
+ {
+ bool eventFired = false;
+ Thermostat thermostat = new Thermostat();
+ thermostat.PropertyChanged += (object? sender, System.EventArgs e) =>
+ {
+ eventFired = true;
+ };
+ thermostat.Humidity = 42;
+ Assert.IsTrue(eventFired);
+ }
+ }
+}
\ No newline at end of file

MODIFY
src/Chapter04/Chapter04.csproj
src/Chapter04/Chapter04.csproj
</PropertyGroup>
<PropertyGroup>
<!-- CS1030 results from an explicit #warning preprocessor command -->
- <ExpectedWarnings>1030</ExpectedWarnings>
+ <ExpectedWarnings>$(ExpectedWarnings),1030</ExpectedWarnings>
</PropertyGroup>
<Import Project="..\Chapter.props" />
<ItemGroup>

MODIFY
src/Chapter04/Listing04.19.DeclaringConstants.cs
src/Chapter04/Listing04.19.DeclaringConstants.cs
#pragma warning disable CS0168 // Variable is declared but never used
#pragma warning disable CS0219 // Variable is assigned but its value is never used
+#pragma warning disable IDE0059 // Unnecessary assignment of a value
+
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_19
{
public class Program

MODIFY
src/Chapter04/Listing04.27.OutOfScope.cs
src/Chapter04/Listing04.27.OutOfScope.cs
#pragma warning disable CS0219 // Variable is assigned but its value is never used
-#pragma warning disable CS0168 // Variable is declared but never used
#pragma warning disable IDE0059 // Unnecessary assignment of a value
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_27

MODIFY
src/Chapter04/Listing04.30.AssignAsBool.cs
src/Chapter04/Listing04.30.AssignAsBool.cs
#pragma warning disable CS0168 // Variable is declared but never used
#pragma warning disable CS0219 // Variable is assigned but its value is never used
+#pragma warning disable IDE0059 // Unnecessary assignment of a value
+
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_30
{
public class Program

MODIFY
src/Chapter04/Listing04.36.NullCoalescingOperator.cs
src/Chapter04/Listing04.36.NullCoalescingOperator.cs
#pragma warning disable CS0168 // Variable is declared but never used
#pragma warning disable IDE0059 // Unnecessary assignment of a value
+
using System;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_36

MODIFY
src/Chapter04/Listing04.38.NullConditionalOperatorWithIndexOperator.cs
src/Chapter04/Listing04.38.NullConditionalOperatorWithIndexOperator.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_38
{
- class Program
+ public class Thermostat
{
- public static void Main(string[] args)
+ public event System.EventHandler PropertyChanged = delegate { };
+
+ private int _Temperature;
+ public int Temperature
+ {
+ get { return _Temperature; }
+ set
+ {
+ System.EventHandler propertyChanged =
+ PropertyChanged;
+ if (propertyChanged != null)
{
-#nullable disable
- // CAUTION: args?.Length not verified
- string directoryPath = args?[0];
- string searchPattern = args?[1];
- // ...
+ propertyChanged(this,
+ new System.EventArgs());
+ }
+
+ _Temperature = value;
+ }
}
+ int _Humidity;
+ public int Humidity
+ {
+ get { return _Humidity; }
+ set
+ {
+ PropertyChanged?.Invoke(this,
+ new System.EventArgs());
+
+ _Humidity = value;
+ }
+ }
}
}

MODIFY
src/Chapter04/Listing04.48.InputValidation.cs
src/Chapter04/Listing04.48.InputValidation.cs
#pragma warning disable CS0219 // Variable is assigned but its value is never used
+
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_48
{
public class Program
@@ -38,6 +39,8 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_48
// ...
}
+#pragma warning disable IDE1006 // Promoted to a property to support testing.
public static string input { get; set; } = "Let the game begin!";
+#pragma warning restore IDE1006 // Naming Styles
}
}

MODIFY
src/Chapter04/Listing04.54.GotoStatements.cs
src/Chapter04/Listing04.54.GotoStatements.cs
#pragma warning disable CS0219 // Variable is assigned but its value is never used
+#pragma warning disable IDE0059 // Unnecessary assignment of a value
+
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_54
{
public class Program

MODIFY
src/Chapter04/Listing04.59.PragmaDirective.cs
src/Chapter04/Listing04.59.PragmaDirective.cs
@@ -4,7 +4,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_59
{
public static void Main()
{
-#pragma warning disable 1030
+#pragma warning disable CS1030
}
}
}

MODIFY
src/Chapter04/Listing04.60.PragmaRestore.cs
src/Chapter04/Listing04.60.PragmaRestore.cs
@@ -2,9 +2,11 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter04.Listing04_60
{
public class Program
{
+#pragma warning disable CS1030
public static void Main()
{
-#pragma warning restore 1030
+#warning "Sample warning suppressed by #pragma warning disable."
}
+#pragma warning restore CS1030
}
}

Updated Listing 4.38 to have full implementation; Disable various warnings caused by sample code that was not fully flushed out; Minor updates to #pragma example.