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 +}