Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_19.Tests
{
using Listing14_19;
using static AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_19.Thermostat;

[TestClass]
public class ProgramTests
Expand All @@ -14,7 +15,7 @@ public void AddRemoveHandlerWorks()

float temp = 0;

Thermostat.TemperatureChangeHandler T_OnTemperatureChange = (sender, newTemperature) =>
Thermostat.EventHandler<TemperatureArgs> T_OnTemperatureChange = (sender, newTemperature) =>
{
temp = newTemperature.NewTemperature;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_01
{
class Cooler
#region INCLUDE
public class Cooler
{
public Cooler(float temperature)
{
Temperature = temperature;
}

// Cooler is activated when ambient temperature is higher than this
// Cooler is activated when ambient temperature
// is higher than this
public float Temperature { get; set; }

// Notifies that the temperature changed on this instance
Expand All @@ -24,14 +26,15 @@ public void OnTemperatureChanged(float newTemperature)
}
}

class Heater
public class Heater
{
public Heater(float temperature)
{
Temperature = temperature;
}

// Cooler is activated when ambient temperature is higher than this
// Heater is activated when ambient temperature
// is lower than this
public float Temperature { get; set; }

// Notifies that the temperature changed on this instance
Expand All @@ -47,4 +50,5 @@ public void OnTemperatureChanged(float newTemperature)
}
}
}
#endregion INCLUDE
}
17 changes: 3 additions & 14 deletions src/Chapter14/Listing14.02.DefiningTheEventPublsherThermostat.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_02
{
using System;

#region INCLUDE
public class Thermostat
{
// Using C# 3.0 or later syntax.
// Define the event publisher (initially without the sender)
public Action<float>? OnTemperatureChange { get; set; }

public float CurrentTemperature
{
get { return _CurrentTemperature; }
set
{
if(value != CurrentTemperature)
{
_CurrentTemperature = value;
}
}
}
private float _CurrentTemperature;
public float CurrentTemperature { get; set; }
#endregion INCLUDE
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// TODO: Update listing in Manuscript
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_03
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_03
{
using System;
using Listing14_01;
using Listing14_02;

#region INCLUDE
public class Program
{
public static void Main()
Expand All @@ -13,11 +12,12 @@ public static void Main()
Heater heater = new Heater(60);
Cooler cooler = new Cooler(80);

// Using C# 2.0 or later syntax
#region HIGHLIGHT
thermostat.OnTemperatureChange +=
heater.OnTemperatureChanged;
thermostat.OnTemperatureChange +=
cooler.OnTemperatureChanged;
#endregion HIGHLIGHT

Console.Write("Enter temperature: ");
string? temperature = Console.ReadLine();
Expand All @@ -29,4 +29,5 @@ public static void Main()
thermostat.CurrentTemperature = currentTemperature;
}
}
#endregion INCLUDE
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_04
{
using System;

#region INCLUDE
public class Thermostat
{
#region EXCLUDE
// Define the event publisher
public Action<float>? OnTemperatureChange { get; set; }

#endregion EXCLUDE
public float CurrentTemperature
{
get { return _CurrentTemperature; }
set
{
if(value != CurrentTemperature)
#region HIGHLIGHT
if (value != CurrentTemperature)
#endregion HIGHLIGHT
{
#region HIGHLIGHT
_CurrentTemperature = value;
#endregion HIGHLIGHT

// Call subscribers
// Justification: Incomplete, check for null needed.
// Incomplete, check for null needed
#region EXCLUDE
#pragma warning disable CS8602 // Dereference of a possibly null reference.
#endregion EXCLUDE
OnTemperatureChange(value);
#region EXCLUDE
#pragma warning restore CS8602 // Dereference of a possibly null reference.
#endregion EXCLUDE
}
}
}
private float _CurrentTemperature;
}
#endregion INCLUDE
}
5 changes: 4 additions & 1 deletion src/Chapter14/Listing14.05.InvokingADelegate.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_05
{
using System;

#region INCLUDE
public class Thermostat
{
// Define the event publisher
Expand All @@ -18,11 +18,14 @@ public float CurrentTemperature
// If there are any subscribers,
// notify them of changes in
// temperature by invoking said subcribers
#region HIGHLIGHT
OnTemperatureChange?.Invoke(value); // C# 6.0
#endregion HIGHLIGHT
}
}
}

private float _CurrentTemperature;
}
#endregion INCLUDE
}
9 changes: 8 additions & 1 deletion src/Chapter14/Listing14.06.InvokingADelegatePre6.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_06
{
using System;

#region INCLUDE
public class Thermostat
{
// Define the event publisher
Expand All @@ -18,19 +18,26 @@ public float CurrentTemperature
// If there are any subscribers,
// notify them of changes in
// temperature
#region EXCLUDE
#pragma warning disable IDE1005 // Delegate invocation can be simplified.
#endregion EXCLUDE
#region HIGHLIGHT
Action<float>? localOnChange =
OnTemperatureChange;
if(localOnChange != null)
{
// Call subscribers
localOnChange(value);
}
#endregion HIGHLIGHT
#region EXCLUDE
#pragma warning restore IDE1005 // Delegate invocation can be simplified.
#endregion EXCLUDE
}
}
}

private float _CurrentTemperature;
}
#endregion INCLUDE
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class Program
{
public static void Main()
{
#region INCLUDE
//...
Thermostat thermostat = new Thermostat();
Heater heater = new Heater(60);
Cooler cooler = new Cooler(80);
Expand All @@ -16,18 +18,23 @@ public static void Main()
Action<float> delegate2;
Action<float>? delegate3;

// use Constructor syntax for C# 1.0
delegate1 = heater.OnTemperatureChanged;
delegate2 = cooler.OnTemperatureChanged;

Console.WriteLine("Invoke both delegates:");
delegate3 = delegate1;
#region HIGHLIGHT
delegate3 += delegate2;
#endregion HIGHLIGHT
delegate3(90);

Console.WriteLine("Invoke only delegate2");
#region HIGHLIGHT
delegate3 -= delegate1;
#endregion HIGHLIGHT
delegate3!(30);
//...
#endregion INCLUDE
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
using System;
using Listing14_01;
using Listing14_05;

public class Program
{
public static void Main()
{
#region INCLUDE
//...
Thermostat thermostat = new Thermostat();
Heater heater = new Heater(60);
Cooler cooler = new Cooler(80);
Expand All @@ -22,12 +23,18 @@ public static void Main()
delegate2 = cooler.OnTemperatureChanged;

Console.WriteLine("Combine delegates using + operator:");
#region HIGHLIGHT
delegate3 = delegate1 + delegate2;
#endregion HIGHLIGHT
delegate3(60);

Console.WriteLine("Uncombine delegates using - operator:");
#region HIGHLIGHT
delegate3 = (delegate3 - delegate2)!;
#endregion HIGHLIGHT
delegate3(60);
//...
#endregion INCLUDE
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// TODO: Update listing in Manuscript
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_09
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_09
{
using System;
using Listing14_01;
using Listing14_05;

#region INCLUDE
public class Program
{
public static void Main()
Expand All @@ -13,16 +12,15 @@ public static void Main()
Heater heater = new Heater(60);
Cooler cooler = new Cooler(80);

// Using C# 2.0 or later syntax
thermostat.OnTemperatureChange +=
heater.OnTemperatureChanged;
// Using C# 3.0. Change to anonymous method
// if using C# 2.0
#region HIGHLIGHT
thermostat.OnTemperatureChange +=
(newTemperature) =>
{
throw new InvalidOperationException();
};
#endregion HIGHLIGHT
thermostat.OnTemperatureChange +=
cooler.OnTemperatureChanged;

Expand All @@ -36,4 +34,5 @@ public static void Main()
thermostat.CurrentTemperature = currentTemperature;
}
}
#endregion INCLUDE
}
12 changes: 7 additions & 5 deletions src/Chapter14/Listing14.10.HandlingExceptionsFromSubscribers.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// TODO: Update listing in Manuscript
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_10
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_10
{
using System;
using System.Collections.Generic;
using Listing14_01;

#region INCLUDE
public class Thermostat
{
// Using C# 3.0 or later syntax
// Define the event publisher
public Action<float>? OnTemperatureChange;

Expand All @@ -19,9 +17,11 @@ public float CurrentTemperature
if(value != CurrentTemperature)
{
_CurrentTemperature = value;
Action<float>? onTemperatureChange = OnTemperatureChange;
Action<float>? onTemperatureChange
= OnTemperatureChange;
if (onTemperatureChange != null)
{
#region HIGHLIGHT
List<Exception> exceptionCollection =
new List<Exception>();
foreach(
Expand All @@ -44,12 +44,14 @@ Delegate handler in
"OnTemperatureChange Event subscribers.",
exceptionCollection);
}
#endregion HIGHLIGHT
}
}
}
}
private float _CurrentTemperature;
}
#endregion INCLUDE

public class Program
{
Expand Down
Loading