diff --git a/src/Chapter14.Tests/Listing14.19.CustomAddAndRemoveHandlers.Tests.cs b/src/Chapter14.Tests/Listing14.19.CustomAddAndRemoveHandlers.Tests.cs index ea600963a..72e822768 100644 --- a/src/Chapter14.Tests/Listing14.19.CustomAddAndRemoveHandlers.Tests.cs +++ b/src/Chapter14.Tests/Listing14.19.CustomAddAndRemoveHandlers.Tests.cs @@ -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 @@ -14,7 +15,7 @@ public void AddRemoveHandlerWorks() float temp = 0; - Thermostat.TemperatureChangeHandler T_OnTemperatureChange = (sender, newTemperature) => + Thermostat.EventHandler T_OnTemperatureChange = (sender, newTemperature) => { temp = newTemperature.NewTemperature; }; diff --git a/src/Chapter14/Listing14.01.HeaterAndCoolerEventSubscriberImplementations.cs b/src/Chapter14/Listing14.01.HeaterAndCoolerEventSubscriberImplementations.cs index 31cdf4761..c0297b914 100644 --- a/src/Chapter14/Listing14.01.HeaterAndCoolerEventSubscriberImplementations.cs +++ b/src/Chapter14/Listing14.01.HeaterAndCoolerEventSubscriberImplementations.cs @@ -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 @@ -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 @@ -47,4 +50,5 @@ public void OnTemperatureChanged(float newTemperature) } } } + #endregion INCLUDE } \ No newline at end of file diff --git a/src/Chapter14/Listing14.02.DefiningTheEventPublsherThermostat.cs b/src/Chapter14/Listing14.02.DefiningTheEventPublsherThermostat.cs index 425119bec..b308d3ed2 100644 --- a/src/Chapter14/Listing14.02.DefiningTheEventPublsherThermostat.cs +++ b/src/Chapter14/Listing14.02.DefiningTheEventPublsherThermostat.cs @@ -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? 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 } } \ No newline at end of file diff --git a/src/Chapter14/Listing14.03.ConnectingThePublisherAndSubscribers.cs b/src/Chapter14/Listing14.03.ConnectingThePublisherAndSubscribers.cs index eec3056f5..48c45e686 100644 --- a/src/Chapter14/Listing14.03.ConnectingThePublisherAndSubscribers.cs +++ b/src/Chapter14/Listing14.03.ConnectingThePublisherAndSubscribers.cs @@ -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() @@ -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(); @@ -29,4 +29,5 @@ public static void Main() thermostat.CurrentTemperature = currentTemperature; } } + #endregion INCLUDE } \ No newline at end of file diff --git a/src/Chapter14/Listing14.04.InvokingADelegateWithoutCheckingForNull.cs b/src/Chapter14/Listing14.04.InvokingADelegateWithoutCheckingForNull.cs index 2bcaeaa4d..a4be893fd 100644 --- a/src/Chapter14/Listing14.04.InvokingADelegateWithoutCheckingForNull.cs +++ b/src/Chapter14/Listing14.04.InvokingADelegateWithoutCheckingForNull.cs @@ -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? 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 } \ No newline at end of file diff --git a/src/Chapter14/Listing14.05.InvokingADelegate.cs b/src/Chapter14/Listing14.05.InvokingADelegate.cs index 8af68a523..40177c9f8 100644 --- a/src/Chapter14/Listing14.05.InvokingADelegate.cs +++ b/src/Chapter14/Listing14.05.InvokingADelegate.cs @@ -1,7 +1,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_05 { using System; - + #region INCLUDE public class Thermostat { // Define the event publisher @@ -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 } \ No newline at end of file diff --git a/src/Chapter14/Listing14.06.InvokingADelegatePre6.cs b/src/Chapter14/Listing14.06.InvokingADelegatePre6.cs index e26adb9c9..4b3d0e545 100644 --- a/src/Chapter14/Listing14.06.InvokingADelegatePre6.cs +++ b/src/Chapter14/Listing14.06.InvokingADelegatePre6.cs @@ -1,7 +1,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_06 { using System; - + #region INCLUDE public class Thermostat { // Define the event publisher @@ -18,7 +18,10 @@ 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? localOnChange = OnTemperatureChange; if(localOnChange != null) @@ -26,11 +29,15 @@ public float CurrentTemperature // Call subscribers localOnChange(value); } + #endregion HIGHLIGHT + #region EXCLUDE #pragma warning restore IDE1005 // Delegate invocation can be simplified. + #endregion EXCLUDE } } } private float _CurrentTemperature; } + #endregion INCLUDE } \ No newline at end of file diff --git a/src/Chapter14/Listing14.07.UsingthePlusEqualsAndMinusEqualsDelegateOperators.cs b/src/Chapter14/Listing14.07.UsingthePlusEqualsAndMinusEqualsDelegateOperators.cs index 34f38868b..248610e16 100644 --- a/src/Chapter14/Listing14.07.UsingthePlusEqualsAndMinusEqualsDelegateOperators.cs +++ b/src/Chapter14/Listing14.07.UsingthePlusEqualsAndMinusEqualsDelegateOperators.cs @@ -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); @@ -16,18 +18,23 @@ public static void Main() Action delegate2; Action? 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 } } } \ No newline at end of file diff --git a/src/Chapter14/Listing14.08.UsingThePlusAndMinusDelegateOperators.cs b/src/Chapter14/Listing14.08.UsingThePlusAndMinusDelegateOperators.cs index 321b4a4bd..c9377adc4 100644 --- a/src/Chapter14/Listing14.08.UsingThePlusAndMinusDelegateOperators.cs +++ b/src/Chapter14/Listing14.08.UsingThePlusAndMinusDelegateOperators.cs @@ -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); @@ -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 } } } \ No newline at end of file diff --git a/src/Chapter14/Listing14.09.OnTemperatureChangedThrowingAnException.cs b/src/Chapter14/Listing14.09.OnTemperatureChangedThrowingAnException.cs index d3e48e851..26d076a6e 100644 --- a/src/Chapter14/Listing14.09.OnTemperatureChangedThrowingAnException.cs +++ b/src/Chapter14/Listing14.09.OnTemperatureChangedThrowingAnException.cs @@ -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() @@ -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; @@ -36,4 +34,5 @@ public static void Main() thermostat.CurrentTemperature = currentTemperature; } } + #endregion INCLUDE } \ No newline at end of file diff --git a/src/Chapter14/Listing14.10.HandlingExceptionsFromSubscribers.cs b/src/Chapter14/Listing14.10.HandlingExceptionsFromSubscribers.cs index fb8fff91b..fa6ffc727 100644 --- a/src/Chapter14/Listing14.10.HandlingExceptionsFromSubscribers.cs +++ b/src/Chapter14/Listing14.10.HandlingExceptionsFromSubscribers.cs @@ -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? OnTemperatureChange; @@ -19,9 +17,11 @@ public float CurrentTemperature if(value != CurrentTemperature) { _CurrentTemperature = value; - Action? onTemperatureChange = OnTemperatureChange; + Action? onTemperatureChange + = OnTemperatureChange; if (onTemperatureChange != null) { + #region HIGHLIGHT List exceptionCollection = new List(); foreach( @@ -44,12 +44,14 @@ Delegate handler in "OnTemperatureChange Event subscribers.", exceptionCollection); } + #endregion HIGHLIGHT } } } } private float _CurrentTemperature; } + #endregion INCLUDE public class Program { diff --git a/src/Chapter14/Listing14.11.UsingTheAssignmentOperationRatherThanPlusEquals.cs b/src/Chapter14/Listing14.11.UsingTheAssignmentOperationRatherThanPlusEquals.cs index d0e4441dd..8849d3b91 100644 --- a/src/Chapter14/Listing14.11.UsingTheAssignmentOperationRatherThanPlusEquals.cs +++ b/src/Chapter14/Listing14.11.UsingTheAssignmentOperationRatherThanPlusEquals.cs @@ -1,11 +1,10 @@ -// TODO: Update listing in Manuscript -using AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_02; +using AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_02; namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_11 { using System; using Listing14_01; - + #region INCLUDE public class Program { public static void Main() @@ -14,15 +13,15 @@ public static void Main() Heater heater = new Heater(60); Cooler cooler = new Cooler(80); - // Note: Use new Action(cooler.OnTemperatureChanged) - // for C# 1.0 syntax thermostat.OnTemperatureChange = heater.OnTemperatureChanged; // Bug: Assignment operator overrides // previous assignment + #region HIGHLIGHT thermostat.OnTemperatureChange = cooler.OnTemperatureChanged; + #endregion HIGHLIGHT Console.Write("Enter temperature: "); string? temperature = Console.ReadLine(); @@ -34,4 +33,5 @@ public static void Main() thermostat.CurrentTemperature = currentTemperature; } } + #endregion INCLUDE } \ No newline at end of file diff --git a/src/Chapter14/Listing14.12.FiringTheEventFromOutsideTheEventContainer.cs b/src/Chapter14/Listing14.12.FiringTheEventFromOutsideTheEventContainer.cs index 79e73f662..9c060eccf 100644 --- a/src/Chapter14/Listing14.12.FiringTheEventFromOutsideTheEventContainer.cs +++ b/src/Chapter14/Listing14.12.FiringTheEventFromOutsideTheEventContainer.cs @@ -2,7 +2,7 @@ { using Listing14_01; using Listing14_10; - + #region INCLUDE public class Program { public static void Main() @@ -11,8 +11,6 @@ public static void Main() Heater heater = new Heater(60); Cooler cooler = new Cooler(80); - // Note: Use new Action(cooler.OnTemperatureChanged) - // for C# 1.0 syntax thermostat.OnTemperatureChange += heater.OnTemperatureChanged; @@ -20,7 +18,10 @@ public static void Main() cooler.OnTemperatureChanged; // Bug: Should not be allowed + #region HIGHLIGHT thermostat.OnTemperatureChange(42); + #endregion HIGHLIGHT } } + #endregion INCLUDE } \ No newline at end of file diff --git a/src/Chapter14/Listing14.13.UsingTheEventKeywordWithTheEventCodingPattern.cs b/src/Chapter14/Listing14.13.UsingTheEventKeywordWithTheEventCodingPattern.cs index 016f2aaf8..e8e2372df 100644 --- a/src/Chapter14/Listing14.13.UsingTheEventKeywordWithTheEventCodingPattern.cs +++ b/src/Chapter14/Listing14.13.UsingTheEventKeywordWithTheEventCodingPattern.cs @@ -2,9 +2,10 @@ { using System; #pragma warning disable 67 // OnTemperatureChange is declared but never used - + #region INCLUDE public class Thermostat { + #region HIGHLIGHT public class TemperatureArgs : System.EventArgs { public TemperatureArgs(float newTemperature) @@ -18,13 +19,17 @@ public TemperatureArgs(float newTemperature) // Define the event publisher public event EventHandler OnTemperatureChange = delegate { }; + #endregion HIGHLIGHT public float CurrentTemperature + #region EXCLUDE { get { return _CurrentTemperature; } set { _CurrentTemperature = value; } } + #endregion EXCLUDE private float _CurrentTemperature; } + #endregion INCLUDE #pragma warning restore 67 } \ No newline at end of file diff --git a/src/Chapter14/Listing14.14.DeclaringAGenericDelegateType.cs b/src/Chapter14/Listing14.14.DeclaringAGenericDelegateType.cs index 6547ef1e8..48f9d27f7 100644 --- a/src/Chapter14/Listing14.14.DeclaringAGenericDelegateType.cs +++ b/src/Chapter14/Listing14.14.DeclaringAGenericDelegateType.cs @@ -4,7 +4,10 @@ public class Thermostat { - public delegate void EventHandler(object sender, TEventArgs e) + #region INCLUDE + public delegate void EventHandler( + object sender, TEventArgs e) where TEventArgs : EventArgs; + #endregion INCLUDE } } \ No newline at end of file diff --git a/src/Chapter14/Listing14.15.FiringTheEventNotification.cs b/src/Chapter14/Listing14.15.FiringTheEventNotification.cs index 7f10f4bfa..d147e2089 100644 --- a/src/Chapter14/Listing14.15.FiringTheEventNotification.cs +++ b/src/Chapter14/Listing14.15.FiringTheEventNotification.cs @@ -1,8 +1,9 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_15 { using System; - + #region INCLUDE public class Thermostat + #region EXCLUDE { public class TemperatureArgs : System.EventArgs { @@ -17,6 +18,7 @@ public TemperatureArgs(float newTemperature) // Define the event publisher public event EventHandler OnTemperatureChange = delegate { }; + #endregion EXCLUDE public float CurrentTemperature { @@ -29,11 +31,14 @@ public float CurrentTemperature // If there are any subscribers, // notify them of changes in // temperature by invoking said subcribers + #region HIGHLIGHT OnTemperatureChange?.Invoke( // C# 6.0 this, new TemperatureArgs(value)); + #endregion HIGHLIGHT } } } private float _CurrentTemperature; } + #endregion INCLUDE } \ No newline at end of file diff --git a/src/Chapter14/Listing14.16.UsingCustomDelegateType.cs b/src/Chapter14/Listing14.16.UsingCustomDelegateType.cs index 701f87c95..388fcedc8 100644 --- a/src/Chapter14/Listing14.16.UsingCustomDelegateType.cs +++ b/src/Chapter14/Listing14.16.UsingCustomDelegateType.cs @@ -1,5 +1,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_16 { + #region INCLUDE public class Thermostat { public class TemperatureArgs : System.EventArgs @@ -9,21 +10,19 @@ public TemperatureArgs(float newTemperature) NewTemperature = newTemperature; } - public float NewTemperature - { - get { return _NewTemperature; } - set { _NewTemperature = value; } - } - private float _NewTemperature; + public float NewTemperature { get; set; } } + #region HIGHLIGHT public delegate void TemperatureChangeHandler( object sender, TemperatureArgs newTemperature); public event TemperatureChangeHandler? OnTemperatureChange; + #endregion HIGHLIGHT public float CurrentTemperature + #region EXCLUDE { get { return _CurrentTemperature; } set @@ -40,6 +39,8 @@ public float CurrentTemperature } } } + #endregion EXCLUDE private float _CurrentTemperature; } + #endregion INCLUDE } \ No newline at end of file diff --git a/src/Chapter14/Listing14.17.DeclaringTheOnTemperatureChangeEvent.cs b/src/Chapter14/Listing14.17.DeclaringTheOnTemperatureChangeEvent.cs index a6fff4af3..7b86a5a47 100644 --- a/src/Chapter14/Listing14.17.DeclaringTheOnTemperatureChangeEvent.cs +++ b/src/Chapter14/Listing14.17.DeclaringTheOnTemperatureChangeEvent.cs @@ -5,9 +5,10 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_17 { using System; -// In an actual implementation we would utilize this event - + // In an actual implementation we would utilize this event + #region INCLUDE public class Thermostat + #region EXCLUDE { public class TemperatureArgs : System.EventArgs { @@ -18,7 +19,8 @@ public TemperatureArgs(float newTemperature) public float NewTemperature { get; set; } } - + #endregion EXCLUDE public event EventHandler? OnTemperatureChange; } + #endregion INCLUDE } \ No newline at end of file diff --git a/src/Chapter14/Listing14.18.CSharpConceptualEquivalentOfEventCILCode.cs b/src/Chapter14/Listing14.18.CSharpConceptualEquivalentOfEventCILCode.cs index f64c0ea41..cf1431034 100644 --- a/src/Chapter14/Listing14.18.CSharpConceptualEquivalentOfEventCILCode.cs +++ b/src/Chapter14/Listing14.18.CSharpConceptualEquivalentOfEventCILCode.cs @@ -6,8 +6,9 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_18 { using System; - + #region INCLUDE public class Thermostat + #region EXCLUDE { // Used to suppress warning CS0469: field never assigned to, and // will always have its default value null. @@ -17,6 +18,7 @@ public Thermostat(EventHandler? onTemperatureChange) } // ... + #endregion EXCLUDE // Declaring the delegate field to save the // list of subscribers private EventHandler? _OnTemperatureChange; @@ -33,19 +35,22 @@ public void remove_OnTemperatureChange( System.Delegate.Remove(_OnTemperatureChange, handler); } - //public event EventHandler OnTemperatureChange - //{ - // //Would cause a compiler error - // add - // { - // add_OnTemperatureChange(value); - // } - // //Would cause a compiler error - // remove - // { - // remove_OnTemperatureChange(value); - // } - //} + #if ConceptualEquivalentCode + public event EventHandler OnTemperatureChange + { + //Would cause a compiler error + add + { + add_OnTemperatureChange(value); + } + //Would cause a compiler error + remove + { + remove_OnTemperatureChange(value); + } + } + #endif // ConceptualEquivalentCode + #endregion INCLUDE public class TemperatureArgs : System.EventArgs { diff --git a/src/Chapter14/Listing14.19.CustomAddAndRemoveHandlers.cs b/src/Chapter14/Listing14.19.CustomAddAndRemoveHandlers.cs index 95d4997f9..c5e96db1b 100644 --- a/src/Chapter14/Listing14.19.CustomAddAndRemoveHandlers.cs +++ b/src/Chapter14/Listing14.19.CustomAddAndRemoveHandlers.cs @@ -1,8 +1,10 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_19 { + #region INCLUDE public class Thermostat { public class TemperatureArgs : System.EventArgs + #region EXCLUDE { public TemperatureArgs(float newTemperature) { @@ -11,27 +13,33 @@ public TemperatureArgs(float newTemperature) public float NewTemperature { get; set; } } - + // Define the delegate data type - public delegate void TemperatureChangeHandler( + public delegate void EventHandler( object sender, TemperatureArgs newTemperature); - + #endregion EXCLUDE + #region HIGHLIGHT // Define the event publisher - public event TemperatureChangeHandler OnTemperatureChange + public event EventHandler OnTemperatureChange { add { - _OnTemperatureChange = (TemperatureChangeHandler)System.Delegate.Combine(value, _OnTemperatureChange); + _OnTemperatureChange = + (EventHandler) + System.Delegate.Combine(value, _OnTemperatureChange); } remove { _OnTemperatureChange = - (TemperatureChangeHandler?)System.Delegate.Remove(_OnTemperatureChange, value); + (EventHandler?) + System.Delegate.Remove(_OnTemperatureChange, value); } } - protected TemperatureChangeHandler? _OnTemperatureChange; + protected EventHandler? _OnTemperatureChange; + #endregion HIGHLIGHT public float CurrentTemperature + #region EXCLUDE { set { @@ -47,6 +55,8 @@ public float CurrentTemperature } get { return _CurrentTemperature; } } + #endregion EXCLUDE private float _CurrentTemperature; } + #endregion INCLUDE } \ No newline at end of file diff --git a/src/Chapter15/Listing15.01.CollectionInitialization.cs b/src/Chapter15/Listing15.01.CollectionInitialization.cs index 60cd50483..63710d3fa 100644 --- a/src/Chapter15/Listing15.01.CollectionInitialization.cs +++ b/src/Chapter15/Listing15.01.CollectionInitialization.cs @@ -1,5 +1,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_01 { + #region INCLUDE using System; using System.Collections.Generic; @@ -31,4 +32,5 @@ private static void Print(IEnumerable items) } } } + #endregion INCLUDE } diff --git a/src/Chapter15/Listing15.02.DictionaryInitializers.cs b/src/Chapter15/Listing15.02.DictionaryInitializers.cs index 97746dcd5..6077961c7 100644 --- a/src/Chapter15/Listing15.02.DictionaryInitializers.cs +++ b/src/Chapter15/Listing15.02.DictionaryInitializers.cs @@ -1,12 +1,14 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_02 { + #region INCLUDE using System; using System.Collections.Generic; - + #region EXCLUDE public class Program { public static void Main() { + #endregion EXCLUDE #if !PRECSHARP6 Dictionary colorMap = new Dictionary @@ -26,6 +28,7 @@ public static void Main() {"Verbose", ConsoleColor.White} }; #endif + #endregion INCLUDE Print(colorMap); } diff --git a/src/Chapter15/Listing15.03.ForeachWithArrays.cs b/src/Chapter15/Listing15.03.ForeachWithArrays.cs index 165915d71..234c9b700 100644 --- a/src/Chapter15/Listing15.03.ForeachWithArrays.cs +++ b/src/Chapter15/Listing15.03.ForeachWithArrays.cs @@ -6,12 +6,14 @@ public class Program { public static void Main() { + #region INCLUDE int[] array = new int[] { 1, 2, 3, 4, 5, 6 }; foreach(int item in array) { Console.WriteLine(item); } + #endregion INCLUDE } } } diff --git a/src/Chapter15/Listing15.04.CompiledImplementationOfForeachWithArrays.cs b/src/Chapter15/Listing15.04.CompiledImplementationOfForeachWithArrays.cs index d8105c0c6..4325dc0df 100644 --- a/src/Chapter15/Listing15.04.CompiledImplementationOfForeachWithArrays.cs +++ b/src/Chapter15/Listing15.04.CompiledImplementationOfForeachWithArrays.cs @@ -6,6 +6,7 @@ public class Program { public static void Main() { + #region INCLUDE int[] tempArray; int[] array = new int[] { 1, 2, 3, 4, 5, 6 }; @@ -16,6 +17,7 @@ public static void Main() Console.WriteLine(item); } + #endregion INCLUDE } } } diff --git a/src/Chapter15/Listing15.05.IteratingOverACollectionUsingWhile.cs b/src/Chapter15/Listing15.05.IteratingOverACollectionUsingWhile.cs index ad6350aca..0bfee2ac4 100644 --- a/src/Chapter15/Listing15.05.IteratingOverACollectionUsingWhile.cs +++ b/src/Chapter15/Listing15.05.IteratingOverACollectionUsingWhile.cs @@ -6,12 +6,24 @@ public class Program { public static void Main() { + #region INCLUDE System.Collections.Generic.Stack stack = new System.Collections.Generic.Stack(); int number; // ... // This code is conceptual, not the actual code + #if ConceptualCode + while(stack.MoveNext()) + { + number = stack.Current(); + Console.WriteLine(number); + } + #endif // ConceptualCode + + #endregion INCLUDE + + // Actual Code while(stack.Pop() != -1) //this is actually not the right logic, but the point is the while, not stack { number = stack.Peek(); diff --git a/src/Chapter15/Listing15.06.ASeparateEnumeratorMaintainingStateDuringAnIteration.cs b/src/Chapter15/Listing15.06.ASeparateEnumeratorMaintainingStateDuringAnIteration.cs index efa2d3874..6352b15a3 100644 --- a/src/Chapter15/Listing15.06.ASeparateEnumeratorMaintainingStateDuringAnIteration.cs +++ b/src/Chapter15/Listing15.06.ASeparateEnumeratorMaintainingStateDuringAnIteration.cs @@ -6,6 +6,7 @@ public class Program { public static void Main() { + #region INCLUDE System.Collections.Generic.Stack stack = new System.Collections.Generic.Stack(); int number; @@ -23,6 +24,7 @@ public static void Main() number = enumerator.Current; Console.WriteLine(number); } + #endregion INCLUDE } } } diff --git a/src/Chapter15/Listing15.07.CompiledResultOfForeachOnCollections.cs b/src/Chapter15/Listing15.07.CompiledResultOfForeachOnCollections.cs index 0ec3c2689..367390976 100644 --- a/src/Chapter15/Listing15.07.CompiledResultOfForeachOnCollections.cs +++ b/src/Chapter15/Listing15.07.CompiledResultOfForeachOnCollections.cs @@ -6,6 +6,7 @@ public class Program { public static void Main() { + #region INCLUDE System.Collections.Generic.Stack stack = new System.Collections.Generic.Stack(); System.Collections.Generic.Stack.Enumerator enumerator; @@ -35,6 +36,7 @@ public static void Main() // disposable.Dispose(); // } } + #endregion INCLUDE } } } diff --git a/src/Chapter15/Listing15.08.ErrorHandlingAndResourceCleanupWithUsing.cs b/src/Chapter15/Listing15.08.ErrorHandlingAndResourceCleanupWithUsing.cs index eddc670ee..4de6f3bbc 100644 --- a/src/Chapter15/Listing15.08.ErrorHandlingAndResourceCleanupWithUsing.cs +++ b/src/Chapter15/Listing15.08.ErrorHandlingAndResourceCleanupWithUsing.cs @@ -6,20 +6,24 @@ public class Program { public static void Main() { + #region INCLUDE System.Collections.Generic.Stack stack = new System.Collections.Generic.Stack(); int number; - using( + #region HIGHLIGHT + using ( System.Collections.Generic.Stack.Enumerator enumerator = stack.GetEnumerator()) + #endregion HIGHLIGHT { - while(enumerator.MoveNext()) + while (enumerator.MoveNext()) { number = enumerator.Current; Console.WriteLine(number); } } + #endregion INCLUDE } } } diff --git a/src/Chapter15/Listing15.10.FilteringWithSystem.Linq.Enumerable.Where.cs b/src/Chapter15/Listing15.10.FilteringWithSystem.Linq.Enumerable.Where.cs index 1c813b46c..f42508714 100644 --- a/src/Chapter15/Listing15.10.FilteringWithSystem.Linq.Enumerable.Where.cs +++ b/src/Chapter15/Listing15.10.FilteringWithSystem.Linq.Enumerable.Where.cs @@ -1,6 +1,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_10 { using AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15; + #region INCLUDE using System; using System.Collections.Generic; using System.Linq; @@ -10,10 +11,13 @@ public class Program public static void Main() { IEnumerable patents = PatentData.Patents; + #region HIGHLIGHT patents = patents.Where( patent => patent.YearOfPublication.StartsWith("18")); + #endregion HIGHLIGHT Print(patents); } + #region EXCLUDE private static void Print(IEnumerable items) { @@ -22,5 +26,7 @@ private static void Print(IEnumerable items) Console.WriteLine(item); } } + #endregion EXCLUDE } + #endregion INCLUDE } diff --git a/src/Chapter15/Listing15.11.ProjectionWithSystem.Linq.Enumerable.Select.cs b/src/Chapter15/Listing15.11.ProjectionWithSystem.Linq.Enumerable.Select.cs index 21d477fde..ef7e79647 100644 --- a/src/Chapter15/Listing15.11.ProjectionWithSystem.Linq.Enumerable.Select.cs +++ b/src/Chapter15/Listing15.11.ProjectionWithSystem.Linq.Enumerable.Select.cs @@ -1,6 +1,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_11 { using AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15; + #region INCLUDE using System; using System.Collections.Generic; using System.Linq; @@ -12,11 +13,14 @@ public static void Main() IEnumerable patents = PatentData.Patents; IEnumerable patentsOf1800 = patents.Where( patent => patent.YearOfPublication.StartsWith("18")); + #region HIGHLIGHT IEnumerable items = patentsOf1800.Select( patent => patent.ToString()); + #endregion HIGHLIGHT Print(items); } + #region EXCLUDE private static void Print(IEnumerable items) { @@ -25,5 +29,7 @@ private static void Print(IEnumerable items) Console.WriteLine(item); } } + #endregion EXCLUDE } + #endregion INCLUDE } diff --git a/src/Chapter15/Listing15.12.ProjectionWithSystem.Linq.Enumerable.Select.cs b/src/Chapter15/Listing15.12.ProjectionWithSystem.Linq.Enumerable.Select.cs index b0b766144..6f638234c 100644 --- a/src/Chapter15/Listing15.12.ProjectionWithSystem.Linq.Enumerable.Select.cs +++ b/src/Chapter15/Listing15.12.ProjectionWithSystem.Linq.Enumerable.Select.cs @@ -11,12 +11,14 @@ public static void Main() { string rootDirectory = Directory.GetCurrentDirectory(); string searchPattern = "*"; ; - + #region INCLUDE + //... IEnumerable fileList = Directory.EnumerateFiles( rootDirectory, searchPattern); IEnumerable files = fileList.Select( file => new FileInfo(file)); - + //... + #endregion INCLUDE Print(files); } diff --git a/src/Chapter15/Listing15.13.ProjectionToATuple.cs b/src/Chapter15/Listing15.13.ProjectionToATuple.cs index 5c85a7e37..58f6fd7a5 100644 --- a/src/Chapter15/Listing15.13.ProjectionToATuple.cs +++ b/src/Chapter15/Listing15.13.ProjectionToATuple.cs @@ -11,9 +11,11 @@ public static void Main() { string rootDirectory = Directory.GetCurrentDirectory(); string searchPattern = "*"; - + #region INCLUDE + //... IEnumerable fileList = Directory.EnumerateFiles( - rootDirectory, searchPattern); + rootDirectory, searchPattern); + #region HIGHLIGHT IEnumerable<(string FileName, long Size)> items = fileList.Select( file => { @@ -23,8 +25,9 @@ public static void Main() Size: fileInfo.Length ); }); - - + #endregion HIGHLIGHT + //... + #endregion INCLUDE Print(items); } diff --git a/src/Chapter15/Listing15.14.ExecutingLinqQueriesInParallel.cs b/src/Chapter15/Listing15.14.ExecutingLinqQueriesInParallel.cs index 3b92ec443..cfdad9bc2 100644 --- a/src/Chapter15/Listing15.14.ExecutingLinqQueriesInParallel.cs +++ b/src/Chapter15/Listing15.14.ExecutingLinqQueriesInParallel.cs @@ -11,10 +11,13 @@ public static void Main() { string rootDirectory = Directory.GetCurrentDirectory(); string searchPattern = "*"; - - IEnumerable fileList = Directory.GetFiles( + #region INCLUDE + //... + IEnumerable fileList = Directory.EnumerateFiles( rootDirectory, searchPattern); + #region HIGHLIGHT var items = fileList.AsParallel().Select( + #endregion HIGHLIGHT file => { FileInfo fileInfo = new FileInfo(file); @@ -24,10 +27,13 @@ public static void Main() Size = fileInfo.Length }; }); + //... + #endregion INCLUDE Print(items); } + private static void Print(IEnumerable items) { foreach(T item in items) diff --git a/src/Chapter15/Listing15.15.CountingItemsWithCount.cs b/src/Chapter15/Listing15.15.CountingItemsWithCount.cs index c3507ae3f..1d5af52c6 100644 --- a/src/Chapter15/Listing15.15.CountingItemsWithCount.cs +++ b/src/Chapter15/Listing15.15.CountingItemsWithCount.cs @@ -1,6 +1,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_15 { using AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15; + #region INCLUDE using System; using System.Collections.Generic; using System.Linq; @@ -10,11 +11,13 @@ public class Program public static void Main() { IEnumerable patents = PatentData.Patents; + #region HIGHLIGHT Console.WriteLine($"Patent Count: { patents.Count() }"); - Console.WriteLine($@"Patent Count in 1800s: { + #endregion HIGHLIGHT + Console.WriteLine($@"Patent Count in 1800s: { patents.Count(patent => - patent.YearOfPublication.StartsWith("18")) - }"); + patent.YearOfPublication.StartsWith("18"))}"); + #endregion INCLUDE } } } diff --git a/src/Chapter15/Listing15.16.FilteringWithSystem.Linq.Enumerable.Where.cs b/src/Chapter15/Listing15.16.FilteringWithSystem.Linq.Enumerable.Where.cs index 6b078938b..f4876b620 100644 --- a/src/Chapter15/Listing15.16.FilteringWithSystem.Linq.Enumerable.Where.cs +++ b/src/Chapter15/Listing15.16.FilteringWithSystem.Linq.Enumerable.Where.cs @@ -1,14 +1,17 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_16 { using AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15; + #region INCLUDE using System; using System.Collections.Generic; using System.Linq; + #region EXCLUDE public class Program { public static void Main() { + #endregion EXCLUDE IEnumerable patents = PatentData.Patents; bool result; patents = patents.Where( @@ -46,6 +49,9 @@ public static void Main() Console.Write(" There are "); Console.WriteLine( $"{ patents.Count() } patents prior to 1900."); + + //... + #endregion INCLUDE } } } diff --git a/src/Chapter15/Listing15.17.OrderingWithSystem.Linq.Enumerable.OrderByAndThenBy.cs b/src/Chapter15/Listing15.17.OrderingWithSystem.Linq.Enumerable.OrderByAndThenBy.cs index 6dff2aa21..f451bc2ba 100644 --- a/src/Chapter15/Listing15.17.OrderingWithSystem.Linq.Enumerable.OrderByAndThenBy.cs +++ b/src/Chapter15/Listing15.17.OrderingWithSystem.Linq.Enumerable.OrderByAndThenBy.cs @@ -1,14 +1,16 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_17 { using AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15; + #region INCLUDE using System; using System.Collections.Generic; using System.Linq; - + #region EXCLUDE public class Program { public static void Main() { + #endregion EXCLUDE IEnumerable items; Patent[] patents = PatentData.Patents; items = patents.OrderBy( @@ -23,6 +25,9 @@ public static void Main() .ThenByDescending( patent => patent.Title); Print(items); + + //... + #endregion INCLUDE } private static void Print(IEnumerable items) diff --git a/src/Chapter15/Listing15.19.AnInnerJoinUsingSystem.Linq.Enumerable.Join.cs b/src/Chapter15/Listing15.19.AnInnerJoinUsingSystem.Linq.Enumerable.Join.cs index 7f850962e..617316975 100644 --- a/src/Chapter15/Listing15.19.AnInnerJoinUsingSystem.Linq.Enumerable.Join.cs +++ b/src/Chapter15/Listing15.19.AnInnerJoinUsingSystem.Linq.Enumerable.Join.cs @@ -1,18 +1,20 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_19 { using AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15; + #region INCLUDE using System; using System.Collections.Generic; using System.Linq; - + #region EXCLUDE public class Program { public static void Main() { + #endregion EXCLUDE Department[] departments = CorporateData.Departments; Employee[] employees = CorporateData.Employees; - IEnumerable<(int Id, string Name, string Title, + IEnumerable<(int Id, string Name, string Title, Department Department)> items = employees.Join( departments, @@ -29,9 +31,11 @@ public static void Main() foreach ((int Id, string Name, string Title, Department Department) item in items) { Console.WriteLine( - $"{ item.Name } ({ item.Title })"); + $"{item.Name} ({item.Title})"); Console.WriteLine("\t" + item.Department); } } + //... + #endregion INCLUDE } } diff --git a/src/Chapter15/Listing15.20.AnotherInnerJoinWithSystem.Linq.Enumerable.Join.cs b/src/Chapter15/Listing15.20.AnotherInnerJoinWithSystem.Linq.Enumerable.Join.cs index 36c6f99a7..5bceb28ab 100644 --- a/src/Chapter15/Listing15.20.AnotherInnerJoinWithSystem.Linq.Enumerable.Join.cs +++ b/src/Chapter15/Listing15.20.AnotherInnerJoinWithSystem.Linq.Enumerable.Join.cs @@ -1,14 +1,17 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_20 { using AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15; + #region INCLUDE using System; using System.Collections.Generic; using System.Linq; + #region EXCLUDE public class Program { public static void Main() { + #endregion EXCLUDE Department[] departments = CorporateData.Departments; Employee[] employees = CorporateData.Employees; @@ -29,7 +32,8 @@ public static void Main() Console.WriteLine(item.Name); Console.WriteLine("\t" + item.Employee); } - + //... + #endregion INCLUDE } } } diff --git a/src/Chapter15/Listing15.21.GroupingItemsTogetherUsingSystem.Linq.Enumerable.GroupBy.cs b/src/Chapter15/Listing15.21.GroupingItemsTogetherUsingSystem.Linq.Enumerable.GroupBy.cs index e2ec9fe3e..bc3cfd87d 100644 --- a/src/Chapter15/Listing15.21.GroupingItemsTogetherUsingSystem.Linq.Enumerable.GroupBy.cs +++ b/src/Chapter15/Listing15.21.GroupingItemsTogetherUsingSystem.Linq.Enumerable.GroupBy.cs @@ -1,14 +1,16 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_21 { using AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15; + #region INCLUDE using System; using System.Collections.Generic; using System.Linq; - + #region EXCLUDE public class Program { public static void Main() { + #endregion EXCLUDE IEnumerable employees = CorporateData.Employees; IEnumerable> groupedEmployees = @@ -25,7 +27,8 @@ public static void Main() Console.WriteLine( "\tCount: " + employeeGroup.Count()); } - + //... + #endregion INCLUDE } } } diff --git a/src/Chapter15/Listing15.22.CreatingAChildCollectionWithSystem.Linq.Enumerable.GroupJoin.cs b/src/Chapter15/Listing15.22.CreatingAChildCollectionWithSystem.Linq.Enumerable.GroupJoin.cs index 936b12a32..37f03f4e3 100644 --- a/src/Chapter15/Listing15.22.CreatingAChildCollectionWithSystem.Linq.Enumerable.GroupJoin.cs +++ b/src/Chapter15/Listing15.22.CreatingAChildCollectionWithSystem.Linq.Enumerable.GroupJoin.cs @@ -1,14 +1,17 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_22 { using AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15; + #region INCLUDE using System; using System.Collections.Generic; using System.Linq; + #region EXCLUDE public class Program { public static void Main() { + #endregion EXCLUDE Department[] departments = CorporateData.Departments; Employee[] employees = CorporateData.Employees; @@ -23,7 +26,8 @@ public static void Main() departmentEmployees )); - foreach ((_, string name, IEnumerable employeeCollection) in items) + foreach ( + (_, string name, IEnumerable employeeCollection) in items) { Console.WriteLine(name); foreach (Employee employee in employeeCollection) @@ -31,6 +35,8 @@ public static void Main() Console.WriteLine("\t" + employee); } } + //... + #endregion INCLUDE } } } diff --git a/src/Chapter15/Listing15.23.ImplementingAnOuterJoinUsingGroupJoinWithSelectMany.cs b/src/Chapter15/Listing15.23.ImplementingAnOuterJoinUsingGroupJoinWithSelectMany.cs index 8adc433b0..7b94d1f63 100644 --- a/src/Chapter15/Listing15.23.ImplementingAnOuterJoinUsingGroupJoinWithSelectMany.cs +++ b/src/Chapter15/Listing15.23.ImplementingAnOuterJoinUsingGroupJoinWithSelectMany.cs @@ -1,13 +1,16 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_23 { using AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15; + #region INCLUDE using System; using System.Linq; + #region EXCLUDE public class Program { public static void Main() { + #endregion EXCLUDE Department[] departments = CorporateData.Departments; Employee[] employees = CorporateData.Employees; @@ -38,6 +41,8 @@ public static void Main() Console.WriteLine("\t" + employee); } } + //... + #endregion INCLUDE } } } diff --git a/src/Chapter15/Listing15.24.CallingSelectMany.cs b/src/Chapter15/Listing15.24.CallingSelectMany.cs index 087f8bfb6..f8b61b268 100644 --- a/src/Chapter15/Listing15.24.CallingSelectMany.cs +++ b/src/Chapter15/Listing15.24.CallingSelectMany.cs @@ -1,14 +1,17 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_24 { + #region INCLUDE using System; using System.Collections.Generic; using System.Linq; - + #region EXCLUDE public class Program { public static void Main() { - (string Team, string[] Players)[] worldCup2006Finalists = new[] + #endregion EXCLUDE + (string Team, string[] Players) + [] worldCup2006Finalists = new[] { ( TeamName: "France", @@ -53,7 +56,8 @@ public static void Main() team => team.Players); Print(players); - + //... + #endregion INCLUDE } private static void Print(IEnumerable items) diff --git a/src/Chapter15/Listing15.25.MoreSystem.Linq.EnumerableMethodCalls.cs b/src/Chapter15/Listing15.25.MoreSystem.Linq.EnumerableMethodCalls.cs index 4288468a1..b89442cc4 100644 --- a/src/Chapter15/Listing15.25.MoreSystem.Linq.EnumerableMethodCalls.cs +++ b/src/Chapter15/Listing15.25.MoreSystem.Linq.EnumerableMethodCalls.cs @@ -1,5 +1,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_25 { + #region INCLUDE using System; using System.Collections.Generic; using System.Linq; @@ -16,20 +17,30 @@ public static void Main() IEnumerable even = new int[] { 0, 2, 4, 6, 8 }; Print("Even integers: {0}", even); + #region HIGHLIGHT IEnumerable odd = stuff.OfType(); + #endregion HIGHLIGHT Print("Odd integers: {0}", odd); + #region HIGHLIGHT IEnumerable numbers = even.Union(odd); + #endregion HIGHLIGHT Print("Union of odd and even: {0}", numbers); + #region HIGHLIGHT Print("Union with even: {0}", numbers.Union(even)); Print("Concat with odd: {0}", numbers.Concat(odd)); + #endregion HIGHLIGHT Print("Intersection with even: {0}", + #region HIGHLIGHT numbers.Intersect(even)); Print("Distinct: {0}", numbers.Concat(odd).Distinct()); + #endregion HIGHLIGHT + #region HIGHLIGHT if (!numbers.SequenceEqual( numbers.Concat(odd).Distinct())) + #endregion HIGHLIGHT { throw new Exception("Unexpectedly unequal"); } @@ -39,24 +50,27 @@ public static void Main() @"Collection ""SequenceEquals""" + $" {nameof(numbers)}.Concat(odd).Distinct())"); } + #region HIGHLIGHT Print("Reverse: {0}", numbers.Reverse()); Print("Average: {0}", numbers.Average()); Print("Sum: {0}", numbers.Sum()); Print("Max: {0}", numbers.Max()); Print("Min: {0}", numbers.Min()); - + #endregion HIGHLIGHT } - private static void Print - ( + private static void Print( string format, IEnumerable items) where T : notnull => Console.WriteLine(format, string.Join( - ", ", items.Select(x => x.ToString()))); + ", ", items)); + #region EXCLUDE private static void Print(string format, T item) { Console.WriteLine(format, item); } + #endregion EXCLUDE } + #endregion INCLUDE } diff --git a/src/Chapter15/Listing15.26.ImplicitLocalVariablesWithAnonymousTypes.cs b/src/Chapter15/Listing15.26.ImplicitLocalVariablesWithAnonymousTypes.cs index 99360c4c4..401020714 100644 --- a/src/Chapter15/Listing15.26.ImplicitLocalVariablesWithAnonymousTypes.cs +++ b/src/Chapter15/Listing15.26.ImplicitLocalVariablesWithAnonymousTypes.cs @@ -1,11 +1,13 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_26 { + #region INCLUDE using System; public class Program { public static void Main() { + #region HIGHLIGHT var patent1 = new { @@ -25,6 +27,7 @@ public static void Main() // Renamed to show property naming Year = patent1.YearOfPublication }; + #endregion HIGHLIGHT Console.WriteLine( $"{ patent1.Title } ({ patent1.YearOfPublication })"); @@ -40,4 +43,5 @@ public static void Main() Console.WriteLine(patent3); } } + #endregion INCLUDE } diff --git a/src/Chapter15/Listing15.27.ProjectionToAnAnonymousType.cs b/src/Chapter15/Listing15.27.ProjectionToAnAnonymousType.cs index 249dea4b2..195e68515 100644 --- a/src/Chapter15/Listing15.27.ProjectionToAnAnonymousType.cs +++ b/src/Chapter15/Listing15.27.ProjectionToAnAnonymousType.cs @@ -4,16 +4,17 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_27 using System.Collections.Generic; using System.IO; using System.Linq; - public class Program { public static void Main() { string rootDirectory = Directory.GetCurrentDirectory(); string searchPattern = "*"; - + #region INCLUDE + //... IEnumerable fileList = Directory.EnumerateFiles( rootDirectory, searchPattern); + #region HIGHLIGHT var items = fileList.Select( file => { @@ -24,6 +25,9 @@ public static void Main() Size = fileInfo.Length }; }); + #endregion HIGHLIGHT + //... + #endregion INCLUDE Print(items); } diff --git a/src/Chapter15/Listing15.28.TypeSafetyAndImmutabilityOfAnonymousTypes.cs b/src/Chapter15/Listing15.28.TypeSafetyAndImmutabilityOfAnonymousTypes.cs index f04aad5dc..3ddb917c9 100644 --- a/src/Chapter15/Listing15.28.TypeSafetyAndImmutabilityOfAnonymousTypes.cs +++ b/src/Chapter15/Listing15.28.TypeSafetyAndImmutabilityOfAnonymousTypes.cs @@ -1,8 +1,9 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_28 { - class Program + #region INCLUDE + public class Program { - static void Main() + public static void Main() { var patent1 = new @@ -34,7 +35,9 @@ static void Main() // ERROR: Property or indexer 'AnonymousType#1.Title' // cannot be assigned to -- it is read-only' - //patent1.Title = "Swiss Cheese"; //won't compile if uncommented + //patent1.Title = "Swiss Cheese"; + //won't compile if uncommented } } + #endregion INCLUDE } diff --git a/src/Chapter15/Listing15.29.InitializingAnonymousTypeArrays.cs b/src/Chapter15/Listing15.29.InitializingAnonymousTypeArrays.cs index 8fbb409e3..09dcb263d 100644 --- a/src/Chapter15/Listing15.29.InitializingAnonymousTypeArrays.cs +++ b/src/Chapter15/Listing15.29.InitializingAnonymousTypeArrays.cs @@ -1,5 +1,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_29 { + #region INCLUDE using System; using System.Collections.Generic; @@ -42,4 +43,5 @@ private static void Print(IEnumerable items) } } } + #endregion INCLUDE }