From 6484ede8ffc707b41b8ba6a4a456b62203f20051 Mon Sep 17 00:00:00 2001 From: Mark Michaelis Date: Thu, 31 Aug 2023 19:56:12 +0100 Subject: [PATCH 1/2] Added example for static local functions (#530) --- ...ing13.21.StaticAnonymousFunctions.Tests.cs | 17 +++++++ ...ratedByCompilerForOuterVariables.Tests.cs} | 6 +-- ...CapturingLoopVariablesCSharpFive.Tests.cs} | 6 +-- ...riableWorkaroundBeforeCSharpFive.Tests.cs} | 6 +-- ...13.26.ExamingingAnExpressionTree.Tests.cs} | 6 +-- .../Listing13.21.StaticAnonymousFunctions.cs | 50 +++++++++++++++++++ ...deGeneratedByCompilerForOuterVariables.cs} | 2 +- ...13.23.CapturingLoopVariablesCSharpFive.cs} | 2 +- ...LoopVariableWorkaroundBeforeCSharpFive.cs} | 2 +- ...nvertingExpressionTreeToSqlWhereClause.cs} | 4 +- ...isting13.26.ExamingingAnExpressionTree.cs} | 4 +- 11 files changed, 86 insertions(+), 19 deletions(-) create mode 100644 src/Chapter13.Tests/Listing13.21.StaticAnonymousFunctions.Tests.cs rename src/Chapter13.Tests/{Listing13.21.Tests.cs => Listing13.22.CSharpEquivalentOfCILCodeGeneratedByCompilerForOuterVariables.Tests.cs} (82%) rename src/Chapter13.Tests/{Listing13.22.Tests.cs => Listing13.23.CapturingLoopVariablesCSharpFive.Tests.cs} (82%) rename src/Chapter13.Tests/{Listing13.23.Tests.cs => Listing13.24.LoopVariableWorkaroundBeforeCSharpFive.Tests.cs} (83%) rename src/Chapter13.Tests/{Listing13.25.ExamingingAnExpressionTree.Tests.cs => Listing13.26.ExamingingAnExpressionTree.Tests.cs} (89%) create mode 100644 src/Chapter13/Listing13.21.StaticAnonymousFunctions.cs rename src/Chapter13/{Listing13.21.CSharpEquivalentOfCILCodeGeneratedByCompilerForOuterVariables.cs => Listing13.22.CSharpEquivalentOfCILCodeGeneratedByCompilerForOuterVariables.cs} (94%) rename src/Chapter13/{Listing13.22.CapturingLoopVariablesCSharpFive.cs => Listing13.23.CapturingLoopVariablesCSharpFive.cs} (85%) rename src/Chapter13/{Listing13.23.LoopVariableWorkaroundBeforeCSharpFive.cs => Listing13.24.LoopVariableWorkaroundBeforeCSharpFive.cs} (88%) rename src/Chapter13/{Listing13.24.ConvertingExpressionTreeToSqlWhereClause.cs => Listing13.25.ConvertingExpressionTreeToSqlWhereClause.cs} (72%) rename src/Chapter13/{Listing13.25.ExamingingAnExpressionTree.cs => Listing13.26.ExamingingAnExpressionTree.cs} (95%) diff --git a/src/Chapter13.Tests/Listing13.21.StaticAnonymousFunctions.Tests.cs b/src/Chapter13.Tests/Listing13.21.StaticAnonymousFunctions.Tests.cs new file mode 100644 index 000000000..9a7a49cfa --- /dev/null +++ b/src/Chapter13.Tests/Listing13.21.StaticAnonymousFunctions.Tests.cs @@ -0,0 +1,17 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using AddisonWesley.Michaelis.EssentialCSharp.Shared.Tests; + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_21.Tests; + +[TestClass] +public class ProgramTests +{ + [TestMethod] + public async Task StaticAnonymousFunctionBehavior() + { + await CompilerAssert.CompileAsync( + new string[] { "Listing13.21.StaticAnonymousFunctions.cs", + "Listing13.11.UsingADifferentFuncCompatibleMethod.cs"}, + new string[] { "CS8820" } ); + } +} diff --git a/src/Chapter13.Tests/Listing13.21.Tests.cs b/src/Chapter13.Tests/Listing13.22.CSharpEquivalentOfCILCodeGeneratedByCompilerForOuterVariables.Tests.cs similarity index 82% rename from src/Chapter13.Tests/Listing13.21.Tests.cs rename to src/Chapter13.Tests/Listing13.22.CSharpEquivalentOfCILCodeGeneratedByCompilerForOuterVariables.Tests.cs index 546b90cea..b2e7e8e7b 100644 --- a/src/Chapter13.Tests/Listing13.21.Tests.cs +++ b/src/Chapter13.Tests/Listing13.22.CSharpEquivalentOfCILCodeGeneratedByCompilerForOuterVariables.Tests.cs @@ -1,6 +1,6 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_21.Tests; +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_22.Tests; [TestClass] public class ProgramTests @@ -13,4 +13,4 @@ public void MainTest() IntelliTect.TestTools.Console.ConsoleAssert.Expect( expected, Program.Main); } -} \ No newline at end of file +} diff --git a/src/Chapter13.Tests/Listing13.22.Tests.cs b/src/Chapter13.Tests/Listing13.23.CapturingLoopVariablesCSharpFive.Tests.cs similarity index 82% rename from src/Chapter13.Tests/Listing13.22.Tests.cs rename to src/Chapter13.Tests/Listing13.23.CapturingLoopVariablesCSharpFive.Tests.cs index a4b843c07..ea071d8ed 100644 --- a/src/Chapter13.Tests/Listing13.22.Tests.cs +++ b/src/Chapter13.Tests/Listing13.23.CapturingLoopVariablesCSharpFive.Tests.cs @@ -1,6 +1,6 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_22.Tests; +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_23.Tests; [TestClass] public class ProgramTests @@ -13,4 +13,4 @@ public void MainTest() IntelliTect.TestTools.Console.ConsoleAssert.Expect( expected, CaptureLoop.Main); } -} \ No newline at end of file +} diff --git a/src/Chapter13.Tests/Listing13.23.Tests.cs b/src/Chapter13.Tests/Listing13.24.LoopVariableWorkaroundBeforeCSharpFive.Tests.cs similarity index 83% rename from src/Chapter13.Tests/Listing13.23.Tests.cs rename to src/Chapter13.Tests/Listing13.24.LoopVariableWorkaroundBeforeCSharpFive.Tests.cs index fe0054125..af347c445 100644 --- a/src/Chapter13.Tests/Listing13.23.Tests.cs +++ b/src/Chapter13.Tests/Listing13.24.LoopVariableWorkaroundBeforeCSharpFive.Tests.cs @@ -1,6 +1,6 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_23.Tests; +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_24.Tests; [TestClass] public class ProgramTests @@ -13,4 +13,4 @@ public void MainTest() IntelliTect.TestTools.Console.ConsoleAssert.Expect( expected, DoNotCaptureLoop.Main); } -} \ No newline at end of file +} diff --git a/src/Chapter13.Tests/Listing13.25.ExamingingAnExpressionTree.Tests.cs b/src/Chapter13.Tests/Listing13.26.ExamingingAnExpressionTree.Tests.cs similarity index 89% rename from src/Chapter13.Tests/Listing13.25.ExamingingAnExpressionTree.Tests.cs rename to src/Chapter13.Tests/Listing13.26.ExamingingAnExpressionTree.Tests.cs index ad22f01cb..44d948d32 100644 --- a/src/Chapter13.Tests/Listing13.25.ExamingingAnExpressionTree.Tests.cs +++ b/src/Chapter13.Tests/Listing13.26.ExamingingAnExpressionTree.Tests.cs @@ -1,6 +1,6 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_25.Tests; +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_26.Tests; [TestClass] public class ProgramTests @@ -30,4 +30,4 @@ public void Main_ExamingingAnExpressTree() Program.Main(); }); } -} \ No newline at end of file +} diff --git a/src/Chapter13/Listing13.21.StaticAnonymousFunctions.cs b/src/Chapter13/Listing13.21.StaticAnonymousFunctions.cs new file mode 100644 index 000000000..122d08346 --- /dev/null +++ b/src/Chapter13/Listing13.21.StaticAnonymousFunctions.cs @@ -0,0 +1,50 @@ +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_21; + +using System; +using Listing13_11; +public class Program +{ + public static void Main() + { + int[] items = new int[5]; + + for (int i = 0; i < items.Length; i++) + { + Console.Write("Enter an integer:"); + string? text = Console.ReadLine(); + if (!int.TryParse(text, out items[i])) + { + Console.WriteLine($"'{text}' is not a valid integer."); + return; + } + } + + #region INCLUDE + int comparisonCount = 0; + + DelegateSample.BubbleSort(items, + #region HIGHLIGHT + static (int first, int second) => + #endregion HIGHLIGHT + { + #if COMPILEERROR // EXCLUDE + // Error CS8820: A static anonymous function + // cannot contain a reference to comparisonCount. + comparisonCount++; + #endif // COMPILEERROR EXCLUDE + return first < second; + } + ); + + for (int i = 0; i < items.Length; i++) + { + Console.WriteLine(items[i]); + } + + #region HIGHLIGHT + Console.WriteLine("Items were compared {0} times.", + comparisonCount); + #endregion HIGHLIGHT + } +} +#endregion INCLUDE diff --git a/src/Chapter13/Listing13.21.CSharpEquivalentOfCILCodeGeneratedByCompilerForOuterVariables.cs b/src/Chapter13/Listing13.22.CSharpEquivalentOfCILCodeGeneratedByCompilerForOuterVariables.cs similarity index 94% rename from src/Chapter13/Listing13.21.CSharpEquivalentOfCILCodeGeneratedByCompilerForOuterVariables.cs rename to src/Chapter13/Listing13.22.CSharpEquivalentOfCILCodeGeneratedByCompilerForOuterVariables.cs index bd41ee24c..260cb8dbc 100644 --- a/src/Chapter13/Listing13.21.CSharpEquivalentOfCILCodeGeneratedByCompilerForOuterVariables.cs +++ b/src/Chapter13/Listing13.22.CSharpEquivalentOfCILCodeGeneratedByCompilerForOuterVariables.cs @@ -1,4 +1,4 @@ -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_21; +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_22; using System; using Listing13_11; diff --git a/src/Chapter13/Listing13.22.CapturingLoopVariablesCSharpFive.cs b/src/Chapter13/Listing13.23.CapturingLoopVariablesCSharpFive.cs similarity index 85% rename from src/Chapter13/Listing13.22.CapturingLoopVariablesCSharpFive.cs rename to src/Chapter13/Listing13.23.CapturingLoopVariablesCSharpFive.cs index e92297fc7..c31c6a2b2 100644 --- a/src/Chapter13/Listing13.22.CapturingLoopVariablesCSharpFive.cs +++ b/src/Chapter13/Listing13.23.CapturingLoopVariablesCSharpFive.cs @@ -1,4 +1,4 @@ -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_22; +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_23; using System; using System.Collections.Generic; diff --git a/src/Chapter13/Listing13.23.LoopVariableWorkaroundBeforeCSharpFive.cs b/src/Chapter13/Listing13.24.LoopVariableWorkaroundBeforeCSharpFive.cs similarity index 88% rename from src/Chapter13/Listing13.23.LoopVariableWorkaroundBeforeCSharpFive.cs rename to src/Chapter13/Listing13.24.LoopVariableWorkaroundBeforeCSharpFive.cs index d26f027eb..da100430f 100644 --- a/src/Chapter13/Listing13.23.LoopVariableWorkaroundBeforeCSharpFive.cs +++ b/src/Chapter13/Listing13.24.LoopVariableWorkaroundBeforeCSharpFive.cs @@ -1,4 +1,4 @@ -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_23; +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_24; using System; using System.Collections.Generic; diff --git a/src/Chapter13/Listing13.24.ConvertingExpressionTreeToSqlWhereClause.cs b/src/Chapter13/Listing13.25.ConvertingExpressionTreeToSqlWhereClause.cs similarity index 72% rename from src/Chapter13/Listing13.24.ConvertingExpressionTreeToSqlWhereClause.cs rename to src/Chapter13/Listing13.25.ConvertingExpressionTreeToSqlWhereClause.cs index fe5a4d399..9a3b47b62 100644 --- a/src/Chapter13/Listing13.24.ConvertingExpressionTreeToSqlWhereClause.cs +++ b/src/Chapter13/Listing13.25.ConvertingExpressionTreeToSqlWhereClause.cs @@ -1,4 +1,4 @@ -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_24; +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_25; /* #region INCLUDE @@ -8,4 +8,4 @@ SQL WHERE CLAUSE: select * from Person where upper(Name) = 'INIGO MONTOYA'; #endregion INCLUDE - */ \ No newline at end of file + */ diff --git a/src/Chapter13/Listing13.25.ExamingingAnExpressionTree.cs b/src/Chapter13/Listing13.26.ExamingingAnExpressionTree.cs similarity index 95% rename from src/Chapter13/Listing13.25.ExamingingAnExpressionTree.cs rename to src/Chapter13/Listing13.26.ExamingingAnExpressionTree.cs index 0536d1a27..9b1500249 100644 --- a/src/Chapter13/Listing13.25.ExamingingAnExpressionTree.cs +++ b/src/Chapter13/Listing13.26.ExamingingAnExpressionTree.cs @@ -1,4 +1,4 @@ -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_25; +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_26; #region INCLUDE using System; @@ -58,4 +58,4 @@ private static string NodeToString(Expression expression) => " (" + expression.NodeType.ToString() + ")", }; #endregion INCLUDE -} \ No newline at end of file +} From f9e8037f4c8c23cdf4cc5311373444a57914946a Mon Sep 17 00:00:00 2001 From: Mark Michaelis Date: Sun, 3 Sep 2023 00:03:14 +0100 Subject: [PATCH 2/2] Minor edit - Chapter 5&6 (#534) --- src/Chapter05/Listing05.30.RethrowingAnException.cs | 2 +- src/Chapter06/Listing06.06.AccessingFields.cs | 8 ++++---- .../Listing06.39.DisablingRequiredObjectInitialization.cs | 6 ++---- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Chapter05/Listing05.30.RethrowingAnException.cs b/src/Chapter05/Listing05.30.RethrowingAnException.cs index c8555a349..65bf53add 100644 --- a/src/Chapter05/Listing05.30.RethrowingAnException.cs +++ b/src/Chapter05/Listing05.30.RethrowingAnException.cs @@ -17,7 +17,7 @@ public static void Main() catch (FormatException exception) { Console.WriteLine( - "A FormateException was thrown"); + "A FormatException was thrown"); } #region INCLUDE // ... diff --git a/src/Chapter06/Listing06.06.AccessingFields.cs b/src/Chapter06/Listing06.06.AccessingFields.cs index 7bc5c8a6d..ecf72ac02 100644 --- a/src/Chapter06/Listing06.06.AccessingFields.cs +++ b/src/Chapter06/Listing06.06.AccessingFields.cs @@ -18,10 +18,10 @@ public static void Main() employee1.Salary = "Too Little"; IncreaseSalary(employee1); Console.WriteLine( - "{0} {1}: {2}", - employee1.FirstName, - employee1.LastName, - employee1.Salary); + $"{ + employee1.FirstName } { + employee1.LastName }: { + employee1.Salary }"); #endregion HIGHLIGHT // ... } diff --git a/src/Chapter06/Listing06.39.DisablingRequiredObjectInitialization.cs b/src/Chapter06/Listing06.39.DisablingRequiredObjectInitialization.cs index 883411e6e..6dd35fca7 100644 --- a/src/Chapter06/Listing06.39.DisablingRequiredObjectInitialization.cs +++ b/src/Chapter06/Listing06.39.DisablingRequiredObjectInitialization.cs @@ -41,8 +41,7 @@ public required string Title } set { - ArgumentException.ThrowIfNullOrEmpty( - value = value?.Trim()!); + ArgumentException.ThrowIfNullOrEmpty(value); _Title = value; } } @@ -56,8 +55,7 @@ public required string Isbn } set { - ArgumentException.ThrowIfNullOrEmpty( - value = value?.Trim()!); + ArgumentException.ThrowIfNullOrEmpty(value); _Isbn = value; } }