From 290a4dd6a8b4292e4d95e33c9b5d29d3ae2edf26 Mon Sep 17 00:00:00 2001 From: Mark Michaelis Date: Tue, 5 Sep 2023 18:10:53 +0100 Subject: [PATCH 01/15] Update tables in chapter 13 and 3 --- src/Chapter13.Tests/Table03.03.Tests.cs | 47 +++++++++++++++++++ src/Chapter13/Chapter13.csproj | 1 + ...3.01.a.LambdaExpressionNotesAndExamples.cs | 17 +++++++ 3 files changed, 65 insertions(+) create mode 100644 src/Chapter13.Tests/Table03.03.Tests.cs create mode 100644 src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs diff --git a/src/Chapter13.Tests/Table03.03.Tests.cs b/src/Chapter13.Tests/Table03.03.Tests.cs new file mode 100644 index 000000000..b21fd7a6b --- /dev/null +++ b/src/Chapter13.Tests/Table03.03.Tests.cs @@ -0,0 +1,47 @@ +using AddisonWesley.Michaelis.EssentialCSharp.Shared.Tests; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Table03_03.Tests; + +[TestClass] +public class ArrayHighlightsTests +{ + public TestContext TestContext { get; set; } = null!; // Set by MSTest; + +#if !NET6_0_OR_GREATER + [TestMethod] + /* 1. */ [DataRow(".a", "CS8773")] + ///* 2. */ [DataRow(".b", "CS1525", "CS1002", "CS1002", "CS1513", "CS1002", "CS1513", "CS1002")] + ///* 3. */ [DataRow(".c", "CS0270")] + ///* 4. */ [DataRow(".d", "CS1586")] + ///* 5. */ [DataRow(".e", "CS0847")] + ///* 9. */ [DataRow(".i", "CS0847")] + public async Task ParseAndCompile(string fileNameSuffix, params string[] errorIds) + { + await CompilerAssert.CompileAsync($"Table13.01{fileNameSuffix}.LambdaExpressionNotesAndExamples.cs", errorIds); + } + #endif // !NET6_0_OR_GREATER + // 6. + //[TestMethod] + //[ExpectedException(typeof(IndexOutOfRangeException))] + //public void IndexingOffTheEndOfArrayTest() + //{ + // CommonArayCodingErrors.IndexingOffTheEndOfArray(); + //} + + // 7. + //[TestMethod] + //[ExpectedException(typeof(IndexOutOfRangeException))] + //public void Hat0IsOnePastTheEndOfTheArray1Test() + //{ + // CommonArayCodingErrors.Hat0IsOnePastTheEndOfTheArray1(); + //} + + // 8. + //[TestMethod] + //[ExpectedException(typeof(IndexOutOfRangeException))] + //public void Hat0IsOnePastTheEndOfTheArray2Test() + //{ + // CommonArayCodingErrors.LastItemIsLengthMinus1(); + //} +} \ No newline at end of file diff --git a/src/Chapter13/Chapter13.csproj b/src/Chapter13/Chapter13.csproj index ac9e60305..7550d9fc5 100644 --- a/src/Chapter13/Chapter13.csproj +++ b/src/Chapter13/Chapter13.csproj @@ -1,6 +1,7 @@  13 + 9.0 diff --git a/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..cb507442a --- /dev/null +++ b/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,17 @@ +// Justification: Only snippets of source code shown for elucidation. +#pragma warning disable CS0168 // Variable is declared but never used + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; + +public partial class CommonArayCodingErrors +{ + // 1. + static public void SquareBracketsOnVariableRatherThanType() + { +//#if COMPILEERROR +#if !NET6_0_OR_GREATER + var v = (int x) => x; +#endif +//#endif // COMPILEERROR + } +} From 350da278ff3332aa9ff25d743617b0a77b4e8525 Mon Sep 17 00:00:00 2001 From: A-Tanner Date: Fri, 8 Sep 2023 11:49:51 -0700 Subject: [PATCH 02/15] feat: Table 13.01 Code listings and tests written --- src/Chapter13.Tests/Table03.03.Tests.cs | 47 ------------------- src/Chapter13.Tests/Table13.01.Tests.cs | 28 +++++++++++ src/Chapter13/Chapter13.csproj | 2 +- ...3.01.a.LambdaExpressionNotesAndExamples.cs | 10 ++-- ...3.01.b.LambdaExpressionNotesAndExamples.cs | 20 ++++++++ ...3.01.c.LambdaExpressionNotesAndExamples.cs | 19 ++++++++ ...3.01.d.LambdaExpressionNotesAndExamples.cs | 20 ++++++++ ...3.01.e.LambdaExpressionNotesAndExamples.cs | 30 ++++++++++++ ...3.01.f.LambdaExpressionNotesAndExamples.cs | 21 +++++++++ ...3.01.g.LambdaExpressionNotesAndExamples.cs | 24 ++++++++++ ...3.01.h.LambdaExpressionNotesAndExamples.cs | 24 ++++++++++ 11 files changed, 192 insertions(+), 53 deletions(-) delete mode 100644 src/Chapter13.Tests/Table03.03.Tests.cs create mode 100644 src/Chapter13.Tests/Table13.01.Tests.cs create mode 100644 src/Chapter13/Table13.01.b.LambdaExpressionNotesAndExamples.cs create mode 100644 src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs create mode 100644 src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs create mode 100644 src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs create mode 100644 src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs create mode 100644 src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs create mode 100644 src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs diff --git a/src/Chapter13.Tests/Table03.03.Tests.cs b/src/Chapter13.Tests/Table03.03.Tests.cs deleted file mode 100644 index b21fd7a6b..000000000 --- a/src/Chapter13.Tests/Table03.03.Tests.cs +++ /dev/null @@ -1,47 +0,0 @@ -using AddisonWesley.Michaelis.EssentialCSharp.Shared.Tests; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Table03_03.Tests; - -[TestClass] -public class ArrayHighlightsTests -{ - public TestContext TestContext { get; set; } = null!; // Set by MSTest; - -#if !NET6_0_OR_GREATER - [TestMethod] - /* 1. */ [DataRow(".a", "CS8773")] - ///* 2. */ [DataRow(".b", "CS1525", "CS1002", "CS1002", "CS1513", "CS1002", "CS1513", "CS1002")] - ///* 3. */ [DataRow(".c", "CS0270")] - ///* 4. */ [DataRow(".d", "CS1586")] - ///* 5. */ [DataRow(".e", "CS0847")] - ///* 9. */ [DataRow(".i", "CS0847")] - public async Task ParseAndCompile(string fileNameSuffix, params string[] errorIds) - { - await CompilerAssert.CompileAsync($"Table13.01{fileNameSuffix}.LambdaExpressionNotesAndExamples.cs", errorIds); - } - #endif // !NET6_0_OR_GREATER - // 6. - //[TestMethod] - //[ExpectedException(typeof(IndexOutOfRangeException))] - //public void IndexingOffTheEndOfArrayTest() - //{ - // CommonArayCodingErrors.IndexingOffTheEndOfArray(); - //} - - // 7. - //[TestMethod] - //[ExpectedException(typeof(IndexOutOfRangeException))] - //public void Hat0IsOnePastTheEndOfTheArray1Test() - //{ - // CommonArayCodingErrors.Hat0IsOnePastTheEndOfTheArray1(); - //} - - // 8. - //[TestMethod] - //[ExpectedException(typeof(IndexOutOfRangeException))] - //public void Hat0IsOnePastTheEndOfTheArray2Test() - //{ - // CommonArayCodingErrors.LastItemIsLengthMinus1(); - //} -} \ No newline at end of file diff --git a/src/Chapter13.Tests/Table13.01.Tests.cs b/src/Chapter13.Tests/Table13.01.Tests.cs new file mode 100644 index 000000000..9a6dba85d --- /dev/null +++ b/src/Chapter13.Tests/Table13.01.Tests.cs @@ -0,0 +1,28 @@ +using AddisonWesley.Michaelis.EssentialCSharp.Shared.Tests; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01.Tests; + +[TestClass] +public class LambdaHighlightTests +{ + public TestContext TestContext { get; set; } = null!; // Set by MSTest; + + [TestMethod] + /* 1. */ [DataRow(".a", "CS0023")] + /* 2. */ [DataRow(".b", "CS0837")] + /* 3. */ [DataRow(".c", "CS0029", "CS1662")] + /* 5. */ [DataRow(".e", "CS8070", "CS1632")] + /* 6. */ [DataRow(".f", "CS0103")] + /* 7. */ [DataRow(".g", "CS0165")] + /* 8. */ [DataRow(".h", "CS0165")] + public async Task ParseAndCompile(string fileNameSuffix, params string[] errorIds) + { + await CompilerAssert.CompileAsync($"Table13.01{fileNameSuffix}.LambdaExpressionNotesAndExamples.cs", errorIds); + } + + [TestMethod] + public void PatternMatchingOnTypeTest(){ + LambdaExpressionNotesAndExamples.PatternMatchingOnType(); + } +} \ No newline at end of file diff --git a/src/Chapter13/Chapter13.csproj b/src/Chapter13/Chapter13.csproj index 7550d9fc5..f04af9c29 100644 --- a/src/Chapter13/Chapter13.csproj +++ b/src/Chapter13/Chapter13.csproj @@ -1,7 +1,7 @@  13 - 9.0 + 10.0 diff --git a/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs index cb507442a..f3a458211 100644 --- a/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs @@ -3,15 +3,15 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; -public partial class CommonArayCodingErrors +public partial class LambdaExpressionNotesAndExamples { // 1. - static public void SquareBracketsOnVariableRatherThanType() + static public void AccessingMemberMethods() { -//#if COMPILEERROR #if !NET6_0_OR_GREATER - var v = (int x) => x; + //ERROR: Operator "." cannot be applied to + //operand of type "lambda expression" + string s = ((int x) => x).ToString(); #endif -//#endif // COMPILEERROR } } diff --git a/src/Chapter13/Table13.01.b.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.b.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..6ba4d1e3d --- /dev/null +++ b/src/Chapter13/Table13.01.b.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,20 @@ +// Justification: Only snippets of source code shown for elucidation. +#pragma warning disable CS0168 // Variable is declared but never used + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; + +public partial class LambdaExpressionNotesAndExamples +{ + // 2. + static public void PatternMatchingOnType() + { +//#if COMPILEERROR +#if !NET6_0_OR_GREATER + //ERROR: The first operand of an "is" or "as" + //operator may not be a lambda expression or + //anonymous method + bool b = ((int x) => x) is Func; +#endif +//#endif // COMPILEERROR + } +} diff --git a/src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..43b01d222 --- /dev/null +++ b/src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,19 @@ +// Justification: Only snippets of source code shown for elucidation. +#pragma warning disable CS0168 // Variable is declared but never used + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; + +public partial class LambdaExpressionNotesAndExamples +{ + // 3. + static public void ConvertingToImproperDelegate() + { +//#if COMPILEERROR +#if !NET6_0_OR_GREATER + //ERROR: Lambda expression is not compatible + //with Func type + Func f = (int x) => x; +#endif +//#endif // COMPILEERROR + } +} diff --git a/src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..8a6d95ac3 --- /dev/null +++ b/src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,20 @@ +// Justification: Only snippets of source code shown for elucidation. +#pragma warning disable CS0168 // Variable is declared but never used + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; + +public partial class LambdaExpressionNotesAndExamples +{ + // 4. + static public void TypeInferenceOfExpression() + { +//#if COMPILEERROR +#if !NET6_0_OR_GREATER + //ERROR: You cannot assign lambda + //expression to an implicitly + //typed local variable prior C#10 + var v = (int x) => x; +#endif +//#endif // COMPILEERROR + } +} diff --git a/src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..c27ac3e8b --- /dev/null +++ b/src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,30 @@ +// Justification: Only snippets of source code shown for elucidation. +#pragma warning disable CS0168 // Variable is declared but never used + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; + +public partial class LambdaExpressionNotesAndExamples +{ + // 5. + static public void JumpStatementsToOutOfScopeDestinations() + { +//#if COMPILEERROR +#if !NET6_0_OR_GREATER + //ERROR: Control cannot leave the body of an + //anonymous method or lambda expression + string[] args = {"/File", "fileThatMostCertainlyDoesNotExist"}; + Func f; + switch(args[0]) + { + case "/File": + f = () => + { + if (!File.Exists(args[1])) + break; + return args[1]; + }; + } +#endif +//#endif // COMPILEERROR + } +} diff --git a/src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..f3ba95308 --- /dev/null +++ b/src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,21 @@ +// Justification: Only snippets of source code shown for elucidation. +#pragma warning disable CS0168 // Variable is declared but never used + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; + +public partial class LambdaExpressionNotesAndExamples +{ + // 6. + static public void AccessingParametersAndLocalsOutOfBody() + { +//#if COMPILEERROR +#if !NET6_0_OR_GREATER + //ERROR: The name "first" does not + //exist in the current context + Func expression = + (first, second) => first > second; + first++; +#endif +//#endif // COMPILEERROR + } +} diff --git a/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..02ef83033 --- /dev/null +++ b/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,24 @@ +// Justification: Only snippets of source code shown for elucidation. +#pragma warning disable CS0168 // Variable is declared but never used + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; + +public partial class LambdaExpressionNotesAndExamples +{ + // 7. + static public void UsingOutParameters() + { +//#if COMPILEERROR +#if !NET6_0_OR_GREATER + int number; + Func f = + text => int.TryParse(text, out number); + if (f("1")) + { + //ERROR: Use of unassigned local variable + System.Console.Write(number); + } +#endif +//#endif // COMPILEERROR + } +} diff --git a/src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..e7ee77ac3 --- /dev/null +++ b/src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,24 @@ +// Justification: Only snippets of source code shown for elucidation. +#pragma warning disable CS0168 // Variable is declared but never used + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; + +public partial class LambdaExpressionNotesAndExamples +{ + // 8. + static public void CompilerWillNotDetectInLambdaAssignment() + { +//#if COMPILEERROR +#if !NET6_0_OR_GREATER + int number; + Func isFortyTwo = + x => 42 == (number = x); + if(isFortyTwo(42)) + { + //ERROR: Use of unassigned local variable + System.Console.Write(number); + } +#endif +//#endif // COMPILEERROR + } +} From 328f3f3c0e5799c1e57ae2e6315f605a1c0e32dd Mon Sep 17 00:00:00 2001 From: A-Tanner Date: Tue, 12 Sep 2023 13:03:28 -0700 Subject: [PATCH 03/15] fix: TestMethod updated from b to c --- src/Chapter13.Tests/Table13.01.Tests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Chapter13.Tests/Table13.01.Tests.cs b/src/Chapter13.Tests/Table13.01.Tests.cs index 9a6dba85d..02e3e45ee 100644 --- a/src/Chapter13.Tests/Table13.01.Tests.cs +++ b/src/Chapter13.Tests/Table13.01.Tests.cs @@ -22,7 +22,7 @@ public async Task ParseAndCompile(string fileNameSuffix, params string[] errorId } [TestMethod] - public void PatternMatchingOnTypeTest(){ - LambdaExpressionNotesAndExamples.PatternMatchingOnType(); + public void TypeInferenceOfExpressionTest(){ + LambdaExpressionNotesAndExamples.TypeInferenceOfExpression(); } } \ No newline at end of file From 9ba3fca115bc4a44ca2261f797827d662b78ab09 Mon Sep 17 00:00:00 2001 From: A-Tanner Date: Tue, 19 Sep 2023 13:13:50 -0700 Subject: [PATCH 04/15] refactor(wip): Update code listings --- src/Chapter13.Tests/Table13.01.Tests.cs | 22 ++++++++--------- ...3.01.a.LambdaExpressionNotesAndExamples.cs | 7 +++--- ...3.01.b.LambdaExpressionNotesAndExamples.cs | 12 ++++------ ...3.01.c.LambdaExpressionNotesAndExamples.cs | 10 ++++---- ...3.01.d.LambdaExpressionNotesAndExamples.cs | 17 +++++++------ ...3.01.e.LambdaExpressionNotesAndExamples.cs | 20 ++++------------ ...3.01.f.LambdaExpressionNotesAndExamples.cs | 10 ++++---- ...3.01.g.LambdaExpressionNotesAndExamples.cs | 24 ++++++++++++------- ...3.01.h.LambdaExpressionNotesAndExamples.cs | 15 +++++------- ...3.01.i.LambdaExpressionNotesAndExamples.cs | 24 +++++++++++++++++++ ...3.01.j.LambdaExpressionNotesAndExamples.cs | 24 +++++++++++++++++++ 11 files changed, 108 insertions(+), 77 deletions(-) create mode 100644 src/Chapter13/Table13.01.i.LambdaExpressionNotesAndExamples.cs create mode 100644 src/Chapter13/Table13.01.j.LambdaExpressionNotesAndExamples.cs diff --git a/src/Chapter13.Tests/Table13.01.Tests.cs b/src/Chapter13.Tests/Table13.01.Tests.cs index 02e3e45ee..5ebfc0c91 100644 --- a/src/Chapter13.Tests/Table13.01.Tests.cs +++ b/src/Chapter13.Tests/Table13.01.Tests.cs @@ -9,20 +9,18 @@ public class LambdaHighlightTests public TestContext TestContext { get; set; } = null!; // Set by MSTest; [TestMethod] - /* 1. */ [DataRow(".a", "CS0023")] - /* 2. */ [DataRow(".b", "CS0837")] - /* 3. */ [DataRow(".c", "CS0029", "CS1662")] - /* 5. */ [DataRow(".e", "CS8070", "CS1632")] - /* 6. */ [DataRow(".f", "CS0103")] - /* 7. */ [DataRow(".g", "CS0165")] - /* 8. */ [DataRow(".h", "CS0165")] + /* 1. */ [DataRow(".a")] + /* 2. */ [DataRow(".b")] + /* 3. */ [DataRow(".c")] + /* 4. */ [DataRow(".d")] + /* 5. */ [DataRow(".e")] + /* 6. */ [DataRow(".f")] + /* 7. */ [DataRow(".g")] + /* 8. */ [DataRow(".h")] + /* 9. */ [DataRow(".i")] + /* 10. */ [DataRow(".j")] public async Task ParseAndCompile(string fileNameSuffix, params string[] errorIds) { await CompilerAssert.CompileAsync($"Table13.01{fileNameSuffix}.LambdaExpressionNotesAndExamples.cs", errorIds); } - - [TestMethod] - public void TypeInferenceOfExpressionTest(){ - LambdaExpressionNotesAndExamples.TypeInferenceOfExpression(); - } } \ No newline at end of file diff --git a/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs index f3a458211..eeb63f277 100644 --- a/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs @@ -6,12 +6,11 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 1. - static public void AccessingMemberMethods() + static public void DiscardParameters() { #if !NET6_0_OR_GREATER - //ERROR: Operator "." cannot be applied to - //operand of type "lambda expression" - string s = ((int x) => x).ToString(); + Action x = (_,_)=> + Console.WriteLine("This is a test."); #endif } } diff --git a/src/Chapter13/Table13.01.b.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.b.LambdaExpressionNotesAndExamples.cs index 6ba4d1e3d..b9aaa69f6 100644 --- a/src/Chapter13/Table13.01.b.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.b.LambdaExpressionNotesAndExamples.cs @@ -6,15 +6,13 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 2. - static public void PatternMatchingOnType() + static public void TypeInferenceOfExpression() { -//#if COMPILEERROR #if !NET6_0_OR_GREATER - //ERROR: The first operand of an "is" or "as" - //operator may not be a lambda expression or - //anonymous method - bool b = ((int x) => x) is Func; + //You can assign lambda + //expression to an implicitly + //typed local variable starting C#10 + var v = (int x) => x; #endif -//#endif // COMPILEERROR } } diff --git a/src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs index 43b01d222..80c551caf 100644 --- a/src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs @@ -6,14 +6,12 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 3. - static public void ConvertingToImproperDelegate() + static public void ExpressionsCanHaveReturnTypes() { -//#if COMPILEERROR #if !NET6_0_OR_GREATER - //ERROR: Lambda expression is not compatible - //with Func type - Func f = (int x) => x; + Action action = void () => { }; + var func = short?(long number) => number <= short.MaxValue ? (short)number : null; + } #endif -//#endif // COMPILEERROR } } diff --git a/src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs index 8a6d95ac3..bce9b1e33 100644 --- a/src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs @@ -6,15 +6,14 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 4. - static public void TypeInferenceOfExpression() + static public void MemberMethodsOnExpressions() { -//#if COMPILEERROR -#if !NET6_0_OR_GREATER - //ERROR: You cannot assign lambda - //expression to an implicitly - //typed local variable prior C#10 - var v = (int x) => x; -#endif -//#endif // COMPILEERROR + //#if COMPILEERROR + #if !NET6_0_OR_GREATER + //ERROR: Operator "." cannot be applied to + //operand of type "lambda expression" + string s = ((int x) => x).ToString(); + #endif + //#endif // COMPILEERROR } } diff --git a/src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs index c27ac3e8b..5b9a0abea 100644 --- a/src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs @@ -6,24 +6,14 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 5. - static public void JumpStatementsToOutOfScopeDestinations() + static public void PatternMatchingOnType() { //#if COMPILEERROR #if !NET6_0_OR_GREATER - //ERROR: Control cannot leave the body of an - //anonymous method or lambda expression - string[] args = {"/File", "fileThatMostCertainlyDoesNotExist"}; - Func f; - switch(args[0]) - { - case "/File": - f = () => - { - if (!File.Exists(args[1])) - break; - return args[1]; - }; - } + //ERROR: The first operand of an "is" or "as" + //operator may not be a lambda expression or + //anonymous method + bool b = ((int x) => x) is Func; #endif //#endif // COMPILEERROR } diff --git a/src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs index f3ba95308..7547f2e43 100644 --- a/src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs @@ -6,15 +6,13 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 6. - static public void AccessingParametersAndLocalsOutOfBody() + static public void ConvertingToImproperDelegate() { //#if COMPILEERROR #if !NET6_0_OR_GREATER - //ERROR: The name "first" does not - //exist in the current context - Func expression = - (first, second) => first > second; - first++; + //ERROR: Lambda expression is not compatible + //with Func type + Func f = (int x) => x; #endif //#endif // COMPILEERROR } diff --git a/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs index 02ef83033..932b6823d 100644 --- a/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs @@ -6,18 +6,24 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 7. - static public void UsingOutParameters() + static public void JumpStatementsToOutOfScopeDestinations() { //#if COMPILEERROR #if !NET6_0_OR_GREATER - int number; - Func f = - text => int.TryParse(text, out number); - if (f("1")) - { - //ERROR: Use of unassigned local variable - System.Console.Write(number); - } + //ERROR: Control cannot leave the body of an + //anonymous method or lambda expression + string[] args = {"/File", "fileThatMostCertainlyDoesNotExist"}; + Func f; + switch(args[0]) + { + case "/File": + f = () => + { + if (!File.Exists(args[1])) + break; + return args[1]; + }; + } #endif //#endif // COMPILEERROR } diff --git a/src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs index e7ee77ac3..ce168a92f 100644 --- a/src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs @@ -6,18 +6,15 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 8. - static public void CompilerWillNotDetectInLambdaAssignment() + static public void AccessingParametersAndLocalsOutOfBody() { //#if COMPILEERROR #if !NET6_0_OR_GREATER - int number; - Func isFortyTwo = - x => 42 == (number = x); - if(isFortyTwo(42)) - { - //ERROR: Use of unassigned local variable - System.Console.Write(number); - } + //ERROR: The name "first" does not + //exist in the current context + Func expression = + (first, second) => first > second; + first++; #endif //#endif // COMPILEERROR } diff --git a/src/Chapter13/Table13.01.i.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.i.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..7f43cf5c3 --- /dev/null +++ b/src/Chapter13/Table13.01.i.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,24 @@ +// Justification: Only snippets of source code shown for elucidation. +#pragma warning disable CS0168 // Variable is declared but never used + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; + +public partial class LambdaExpressionNotesAndExamples +{ + // 9. + static public void UsingOutParameters() + { +//#if COMPILEERROR +#if !NET6_0_OR_GREATER + int number; + Func f = + text => int.TryParse(text, out number); + if (f("1")) + { + //ERROR: Use of unassigned local variable + System.Console.Write(number); + } +#endif +//#endif // COMPILEERROR + } +} diff --git a/src/Chapter13/Table13.01.j.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.j.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..79c33a677 --- /dev/null +++ b/src/Chapter13/Table13.01.j.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,24 @@ +// Justification: Only snippets of source code shown for elucidation. +#pragma warning disable CS0168 // Variable is declared but never used + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; + +public partial class LambdaExpressionNotesAndExamples +{ + // 10. + static public void CompilerWillNotDetectInLambdaAssignment() + { +//#if COMPILEERROR +#if !NET6_0_OR_GREATER + int number; + Func isFortyTwo = + x => 42 == (number = x); + if(isFortyTwo(42)) + { + //ERROR: Use of unassigned local variable + System.Console.Write(number); + } +#endif +//#endif // COMPILEERROR + } +} From 2c3582bd26101682e3e9c12a41c21d0f5d224ccb Mon Sep 17 00:00:00 2001 From: A-Tanner Date: Tue, 19 Sep 2023 17:27:53 -0700 Subject: [PATCH 05/15] refactor: rewrite listing tests for table 13.01 --- src/Chapter13.Tests/Table13.01.Tests.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Chapter13.Tests/Table13.01.Tests.cs b/src/Chapter13.Tests/Table13.01.Tests.cs index 5ebfc0c91..fc9e80231 100644 --- a/src/Chapter13.Tests/Table13.01.Tests.cs +++ b/src/Chapter13.Tests/Table13.01.Tests.cs @@ -12,13 +12,13 @@ public class LambdaHighlightTests /* 1. */ [DataRow(".a")] /* 2. */ [DataRow(".b")] /* 3. */ [DataRow(".c")] - /* 4. */ [DataRow(".d")] - /* 5. */ [DataRow(".e")] - /* 6. */ [DataRow(".f")] - /* 7. */ [DataRow(".g")] - /* 8. */ [DataRow(".h")] - /* 9. */ [DataRow(".i")] - /* 10. */ [DataRow(".j")] + /* 4. */ [DataRow(".d", "CS0023")] + /* 5. */ [DataRow(".e", "CS0837")] + /* 6. */ [DataRow(".f", "CS0029", "CS1662")] + /* 7. */ [DataRow(".g", "CS8070", "CS1632")] + /* 8. */ [DataRow(".h", "CS0103")] + /* 9. */ [DataRow(".i", "CS0165")] + /* 10. */ [DataRow(".j", "CS0165")] public async Task ParseAndCompile(string fileNameSuffix, params string[] errorIds) { await CompilerAssert.CompileAsync($"Table13.01{fileNameSuffix}.LambdaExpressionNotesAndExamples.cs", errorIds); From 6df05789da153c81643f9b90f3c3e3ad3617c7ce Mon Sep 17 00:00:00 2001 From: Mark Michaelis Date: Tue, 5 Sep 2023 18:10:53 +0100 Subject: [PATCH 06/15] Update tables in chapter 13 and 3 --- src/Chapter13.Tests/Table03.03.Tests.cs | 47 +++++++++++++++++++ src/Chapter13/Chapter13.csproj | 1 + ...3.01.a.LambdaExpressionNotesAndExamples.cs | 17 +++++++ 3 files changed, 65 insertions(+) create mode 100644 src/Chapter13.Tests/Table03.03.Tests.cs create mode 100644 src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs diff --git a/src/Chapter13.Tests/Table03.03.Tests.cs b/src/Chapter13.Tests/Table03.03.Tests.cs new file mode 100644 index 000000000..b21fd7a6b --- /dev/null +++ b/src/Chapter13.Tests/Table03.03.Tests.cs @@ -0,0 +1,47 @@ +using AddisonWesley.Michaelis.EssentialCSharp.Shared.Tests; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Table03_03.Tests; + +[TestClass] +public class ArrayHighlightsTests +{ + public TestContext TestContext { get; set; } = null!; // Set by MSTest; + +#if !NET6_0_OR_GREATER + [TestMethod] + /* 1. */ [DataRow(".a", "CS8773")] + ///* 2. */ [DataRow(".b", "CS1525", "CS1002", "CS1002", "CS1513", "CS1002", "CS1513", "CS1002")] + ///* 3. */ [DataRow(".c", "CS0270")] + ///* 4. */ [DataRow(".d", "CS1586")] + ///* 5. */ [DataRow(".e", "CS0847")] + ///* 9. */ [DataRow(".i", "CS0847")] + public async Task ParseAndCompile(string fileNameSuffix, params string[] errorIds) + { + await CompilerAssert.CompileAsync($"Table13.01{fileNameSuffix}.LambdaExpressionNotesAndExamples.cs", errorIds); + } + #endif // !NET6_0_OR_GREATER + // 6. + //[TestMethod] + //[ExpectedException(typeof(IndexOutOfRangeException))] + //public void IndexingOffTheEndOfArrayTest() + //{ + // CommonArayCodingErrors.IndexingOffTheEndOfArray(); + //} + + // 7. + //[TestMethod] + //[ExpectedException(typeof(IndexOutOfRangeException))] + //public void Hat0IsOnePastTheEndOfTheArray1Test() + //{ + // CommonArayCodingErrors.Hat0IsOnePastTheEndOfTheArray1(); + //} + + // 8. + //[TestMethod] + //[ExpectedException(typeof(IndexOutOfRangeException))] + //public void Hat0IsOnePastTheEndOfTheArray2Test() + //{ + // CommonArayCodingErrors.LastItemIsLengthMinus1(); + //} +} \ No newline at end of file diff --git a/src/Chapter13/Chapter13.csproj b/src/Chapter13/Chapter13.csproj index ac9e60305..7550d9fc5 100644 --- a/src/Chapter13/Chapter13.csproj +++ b/src/Chapter13/Chapter13.csproj @@ -1,6 +1,7 @@  13 + 9.0 diff --git a/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..cb507442a --- /dev/null +++ b/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,17 @@ +// Justification: Only snippets of source code shown for elucidation. +#pragma warning disable CS0168 // Variable is declared but never used + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; + +public partial class CommonArayCodingErrors +{ + // 1. + static public void SquareBracketsOnVariableRatherThanType() + { +//#if COMPILEERROR +#if !NET6_0_OR_GREATER + var v = (int x) => x; +#endif +//#endif // COMPILEERROR + } +} From a9e1359220612c0158228bff69ed94771853eb18 Mon Sep 17 00:00:00 2001 From: A-Tanner Date: Fri, 8 Sep 2023 11:49:51 -0700 Subject: [PATCH 07/15] feat: Table 13.01 Code listings and tests written --- src/Chapter13.Tests/Table03.03.Tests.cs | 47 ------------------- src/Chapter13.Tests/Table13.01.Tests.cs | 28 +++++++++++ src/Chapter13/Chapter13.csproj | 2 +- ...3.01.a.LambdaExpressionNotesAndExamples.cs | 10 ++-- ...3.01.b.LambdaExpressionNotesAndExamples.cs | 20 ++++++++ ...3.01.c.LambdaExpressionNotesAndExamples.cs | 19 ++++++++ ...3.01.d.LambdaExpressionNotesAndExamples.cs | 20 ++++++++ ...3.01.e.LambdaExpressionNotesAndExamples.cs | 30 ++++++++++++ ...3.01.f.LambdaExpressionNotesAndExamples.cs | 21 +++++++++ ...3.01.g.LambdaExpressionNotesAndExamples.cs | 24 ++++++++++ ...3.01.h.LambdaExpressionNotesAndExamples.cs | 24 ++++++++++ 11 files changed, 192 insertions(+), 53 deletions(-) delete mode 100644 src/Chapter13.Tests/Table03.03.Tests.cs create mode 100644 src/Chapter13.Tests/Table13.01.Tests.cs create mode 100644 src/Chapter13/Table13.01.b.LambdaExpressionNotesAndExamples.cs create mode 100644 src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs create mode 100644 src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs create mode 100644 src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs create mode 100644 src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs create mode 100644 src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs create mode 100644 src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs diff --git a/src/Chapter13.Tests/Table03.03.Tests.cs b/src/Chapter13.Tests/Table03.03.Tests.cs deleted file mode 100644 index b21fd7a6b..000000000 --- a/src/Chapter13.Tests/Table03.03.Tests.cs +++ /dev/null @@ -1,47 +0,0 @@ -using AddisonWesley.Michaelis.EssentialCSharp.Shared.Tests; -using Microsoft.VisualStudio.TestTools.UnitTesting; - -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter03.Table03_03.Tests; - -[TestClass] -public class ArrayHighlightsTests -{ - public TestContext TestContext { get; set; } = null!; // Set by MSTest; - -#if !NET6_0_OR_GREATER - [TestMethod] - /* 1. */ [DataRow(".a", "CS8773")] - ///* 2. */ [DataRow(".b", "CS1525", "CS1002", "CS1002", "CS1513", "CS1002", "CS1513", "CS1002")] - ///* 3. */ [DataRow(".c", "CS0270")] - ///* 4. */ [DataRow(".d", "CS1586")] - ///* 5. */ [DataRow(".e", "CS0847")] - ///* 9. */ [DataRow(".i", "CS0847")] - public async Task ParseAndCompile(string fileNameSuffix, params string[] errorIds) - { - await CompilerAssert.CompileAsync($"Table13.01{fileNameSuffix}.LambdaExpressionNotesAndExamples.cs", errorIds); - } - #endif // !NET6_0_OR_GREATER - // 6. - //[TestMethod] - //[ExpectedException(typeof(IndexOutOfRangeException))] - //public void IndexingOffTheEndOfArrayTest() - //{ - // CommonArayCodingErrors.IndexingOffTheEndOfArray(); - //} - - // 7. - //[TestMethod] - //[ExpectedException(typeof(IndexOutOfRangeException))] - //public void Hat0IsOnePastTheEndOfTheArray1Test() - //{ - // CommonArayCodingErrors.Hat0IsOnePastTheEndOfTheArray1(); - //} - - // 8. - //[TestMethod] - //[ExpectedException(typeof(IndexOutOfRangeException))] - //public void Hat0IsOnePastTheEndOfTheArray2Test() - //{ - // CommonArayCodingErrors.LastItemIsLengthMinus1(); - //} -} \ No newline at end of file diff --git a/src/Chapter13.Tests/Table13.01.Tests.cs b/src/Chapter13.Tests/Table13.01.Tests.cs new file mode 100644 index 000000000..9a6dba85d --- /dev/null +++ b/src/Chapter13.Tests/Table13.01.Tests.cs @@ -0,0 +1,28 @@ +using AddisonWesley.Michaelis.EssentialCSharp.Shared.Tests; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01.Tests; + +[TestClass] +public class LambdaHighlightTests +{ + public TestContext TestContext { get; set; } = null!; // Set by MSTest; + + [TestMethod] + /* 1. */ [DataRow(".a", "CS0023")] + /* 2. */ [DataRow(".b", "CS0837")] + /* 3. */ [DataRow(".c", "CS0029", "CS1662")] + /* 5. */ [DataRow(".e", "CS8070", "CS1632")] + /* 6. */ [DataRow(".f", "CS0103")] + /* 7. */ [DataRow(".g", "CS0165")] + /* 8. */ [DataRow(".h", "CS0165")] + public async Task ParseAndCompile(string fileNameSuffix, params string[] errorIds) + { + await CompilerAssert.CompileAsync($"Table13.01{fileNameSuffix}.LambdaExpressionNotesAndExamples.cs", errorIds); + } + + [TestMethod] + public void PatternMatchingOnTypeTest(){ + LambdaExpressionNotesAndExamples.PatternMatchingOnType(); + } +} \ No newline at end of file diff --git a/src/Chapter13/Chapter13.csproj b/src/Chapter13/Chapter13.csproj index 7550d9fc5..f04af9c29 100644 --- a/src/Chapter13/Chapter13.csproj +++ b/src/Chapter13/Chapter13.csproj @@ -1,7 +1,7 @@  13 - 9.0 + 10.0 diff --git a/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs index cb507442a..f3a458211 100644 --- a/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs @@ -3,15 +3,15 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; -public partial class CommonArayCodingErrors +public partial class LambdaExpressionNotesAndExamples { // 1. - static public void SquareBracketsOnVariableRatherThanType() + static public void AccessingMemberMethods() { -//#if COMPILEERROR #if !NET6_0_OR_GREATER - var v = (int x) => x; + //ERROR: Operator "." cannot be applied to + //operand of type "lambda expression" + string s = ((int x) => x).ToString(); #endif -//#endif // COMPILEERROR } } diff --git a/src/Chapter13/Table13.01.b.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.b.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..6ba4d1e3d --- /dev/null +++ b/src/Chapter13/Table13.01.b.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,20 @@ +// Justification: Only snippets of source code shown for elucidation. +#pragma warning disable CS0168 // Variable is declared but never used + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; + +public partial class LambdaExpressionNotesAndExamples +{ + // 2. + static public void PatternMatchingOnType() + { +//#if COMPILEERROR +#if !NET6_0_OR_GREATER + //ERROR: The first operand of an "is" or "as" + //operator may not be a lambda expression or + //anonymous method + bool b = ((int x) => x) is Func; +#endif +//#endif // COMPILEERROR + } +} diff --git a/src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..43b01d222 --- /dev/null +++ b/src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,19 @@ +// Justification: Only snippets of source code shown for elucidation. +#pragma warning disable CS0168 // Variable is declared but never used + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; + +public partial class LambdaExpressionNotesAndExamples +{ + // 3. + static public void ConvertingToImproperDelegate() + { +//#if COMPILEERROR +#if !NET6_0_OR_GREATER + //ERROR: Lambda expression is not compatible + //with Func type + Func f = (int x) => x; +#endif +//#endif // COMPILEERROR + } +} diff --git a/src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..8a6d95ac3 --- /dev/null +++ b/src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,20 @@ +// Justification: Only snippets of source code shown for elucidation. +#pragma warning disable CS0168 // Variable is declared but never used + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; + +public partial class LambdaExpressionNotesAndExamples +{ + // 4. + static public void TypeInferenceOfExpression() + { +//#if COMPILEERROR +#if !NET6_0_OR_GREATER + //ERROR: You cannot assign lambda + //expression to an implicitly + //typed local variable prior C#10 + var v = (int x) => x; +#endif +//#endif // COMPILEERROR + } +} diff --git a/src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..c27ac3e8b --- /dev/null +++ b/src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,30 @@ +// Justification: Only snippets of source code shown for elucidation. +#pragma warning disable CS0168 // Variable is declared but never used + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; + +public partial class LambdaExpressionNotesAndExamples +{ + // 5. + static public void JumpStatementsToOutOfScopeDestinations() + { +//#if COMPILEERROR +#if !NET6_0_OR_GREATER + //ERROR: Control cannot leave the body of an + //anonymous method or lambda expression + string[] args = {"/File", "fileThatMostCertainlyDoesNotExist"}; + Func f; + switch(args[0]) + { + case "/File": + f = () => + { + if (!File.Exists(args[1])) + break; + return args[1]; + }; + } +#endif +//#endif // COMPILEERROR + } +} diff --git a/src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..f3ba95308 --- /dev/null +++ b/src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,21 @@ +// Justification: Only snippets of source code shown for elucidation. +#pragma warning disable CS0168 // Variable is declared but never used + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; + +public partial class LambdaExpressionNotesAndExamples +{ + // 6. + static public void AccessingParametersAndLocalsOutOfBody() + { +//#if COMPILEERROR +#if !NET6_0_OR_GREATER + //ERROR: The name "first" does not + //exist in the current context + Func expression = + (first, second) => first > second; + first++; +#endif +//#endif // COMPILEERROR + } +} diff --git a/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..02ef83033 --- /dev/null +++ b/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,24 @@ +// Justification: Only snippets of source code shown for elucidation. +#pragma warning disable CS0168 // Variable is declared but never used + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; + +public partial class LambdaExpressionNotesAndExamples +{ + // 7. + static public void UsingOutParameters() + { +//#if COMPILEERROR +#if !NET6_0_OR_GREATER + int number; + Func f = + text => int.TryParse(text, out number); + if (f("1")) + { + //ERROR: Use of unassigned local variable + System.Console.Write(number); + } +#endif +//#endif // COMPILEERROR + } +} diff --git a/src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..e7ee77ac3 --- /dev/null +++ b/src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,24 @@ +// Justification: Only snippets of source code shown for elucidation. +#pragma warning disable CS0168 // Variable is declared but never used + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; + +public partial class LambdaExpressionNotesAndExamples +{ + // 8. + static public void CompilerWillNotDetectInLambdaAssignment() + { +//#if COMPILEERROR +#if !NET6_0_OR_GREATER + int number; + Func isFortyTwo = + x => 42 == (number = x); + if(isFortyTwo(42)) + { + //ERROR: Use of unassigned local variable + System.Console.Write(number); + } +#endif +//#endif // COMPILEERROR + } +} From 004ab7b7eccfc43b9fc5476749fcbd20f17ab5bd Mon Sep 17 00:00:00 2001 From: A-Tanner Date: Tue, 12 Sep 2023 13:03:28 -0700 Subject: [PATCH 08/15] fix: TestMethod updated from b to c --- src/Chapter13.Tests/Table13.01.Tests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Chapter13.Tests/Table13.01.Tests.cs b/src/Chapter13.Tests/Table13.01.Tests.cs index 9a6dba85d..02e3e45ee 100644 --- a/src/Chapter13.Tests/Table13.01.Tests.cs +++ b/src/Chapter13.Tests/Table13.01.Tests.cs @@ -22,7 +22,7 @@ public async Task ParseAndCompile(string fileNameSuffix, params string[] errorId } [TestMethod] - public void PatternMatchingOnTypeTest(){ - LambdaExpressionNotesAndExamples.PatternMatchingOnType(); + public void TypeInferenceOfExpressionTest(){ + LambdaExpressionNotesAndExamples.TypeInferenceOfExpression(); } } \ No newline at end of file From 34f983b88179c23b28c82406ee5f20008c4256be Mon Sep 17 00:00:00 2001 From: A-Tanner Date: Tue, 19 Sep 2023 13:13:50 -0700 Subject: [PATCH 09/15] refactor(wip): Update code listings --- src/Chapter13.Tests/Table13.01.Tests.cs | 22 ++++++++--------- ...3.01.a.LambdaExpressionNotesAndExamples.cs | 7 +++--- ...3.01.b.LambdaExpressionNotesAndExamples.cs | 12 ++++------ ...3.01.c.LambdaExpressionNotesAndExamples.cs | 10 ++++---- ...3.01.d.LambdaExpressionNotesAndExamples.cs | 17 +++++++------ ...3.01.e.LambdaExpressionNotesAndExamples.cs | 20 ++++------------ ...3.01.f.LambdaExpressionNotesAndExamples.cs | 10 ++++---- ...3.01.g.LambdaExpressionNotesAndExamples.cs | 24 ++++++++++++------- ...3.01.h.LambdaExpressionNotesAndExamples.cs | 15 +++++------- ...3.01.i.LambdaExpressionNotesAndExamples.cs | 24 +++++++++++++++++++ ...3.01.j.LambdaExpressionNotesAndExamples.cs | 24 +++++++++++++++++++ 11 files changed, 108 insertions(+), 77 deletions(-) create mode 100644 src/Chapter13/Table13.01.i.LambdaExpressionNotesAndExamples.cs create mode 100644 src/Chapter13/Table13.01.j.LambdaExpressionNotesAndExamples.cs diff --git a/src/Chapter13.Tests/Table13.01.Tests.cs b/src/Chapter13.Tests/Table13.01.Tests.cs index 02e3e45ee..5ebfc0c91 100644 --- a/src/Chapter13.Tests/Table13.01.Tests.cs +++ b/src/Chapter13.Tests/Table13.01.Tests.cs @@ -9,20 +9,18 @@ public class LambdaHighlightTests public TestContext TestContext { get; set; } = null!; // Set by MSTest; [TestMethod] - /* 1. */ [DataRow(".a", "CS0023")] - /* 2. */ [DataRow(".b", "CS0837")] - /* 3. */ [DataRow(".c", "CS0029", "CS1662")] - /* 5. */ [DataRow(".e", "CS8070", "CS1632")] - /* 6. */ [DataRow(".f", "CS0103")] - /* 7. */ [DataRow(".g", "CS0165")] - /* 8. */ [DataRow(".h", "CS0165")] + /* 1. */ [DataRow(".a")] + /* 2. */ [DataRow(".b")] + /* 3. */ [DataRow(".c")] + /* 4. */ [DataRow(".d")] + /* 5. */ [DataRow(".e")] + /* 6. */ [DataRow(".f")] + /* 7. */ [DataRow(".g")] + /* 8. */ [DataRow(".h")] + /* 9. */ [DataRow(".i")] + /* 10. */ [DataRow(".j")] public async Task ParseAndCompile(string fileNameSuffix, params string[] errorIds) { await CompilerAssert.CompileAsync($"Table13.01{fileNameSuffix}.LambdaExpressionNotesAndExamples.cs", errorIds); } - - [TestMethod] - public void TypeInferenceOfExpressionTest(){ - LambdaExpressionNotesAndExamples.TypeInferenceOfExpression(); - } } \ No newline at end of file diff --git a/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs index f3a458211..eeb63f277 100644 --- a/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs @@ -6,12 +6,11 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 1. - static public void AccessingMemberMethods() + static public void DiscardParameters() { #if !NET6_0_OR_GREATER - //ERROR: Operator "." cannot be applied to - //operand of type "lambda expression" - string s = ((int x) => x).ToString(); + Action x = (_,_)=> + Console.WriteLine("This is a test."); #endif } } diff --git a/src/Chapter13/Table13.01.b.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.b.LambdaExpressionNotesAndExamples.cs index 6ba4d1e3d..b9aaa69f6 100644 --- a/src/Chapter13/Table13.01.b.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.b.LambdaExpressionNotesAndExamples.cs @@ -6,15 +6,13 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 2. - static public void PatternMatchingOnType() + static public void TypeInferenceOfExpression() { -//#if COMPILEERROR #if !NET6_0_OR_GREATER - //ERROR: The first operand of an "is" or "as" - //operator may not be a lambda expression or - //anonymous method - bool b = ((int x) => x) is Func; + //You can assign lambda + //expression to an implicitly + //typed local variable starting C#10 + var v = (int x) => x; #endif -//#endif // COMPILEERROR } } diff --git a/src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs index 43b01d222..80c551caf 100644 --- a/src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs @@ -6,14 +6,12 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 3. - static public void ConvertingToImproperDelegate() + static public void ExpressionsCanHaveReturnTypes() { -//#if COMPILEERROR #if !NET6_0_OR_GREATER - //ERROR: Lambda expression is not compatible - //with Func type - Func f = (int x) => x; + Action action = void () => { }; + var func = short?(long number) => number <= short.MaxValue ? (short)number : null; + } #endif -//#endif // COMPILEERROR } } diff --git a/src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs index 8a6d95ac3..bce9b1e33 100644 --- a/src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs @@ -6,15 +6,14 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 4. - static public void TypeInferenceOfExpression() + static public void MemberMethodsOnExpressions() { -//#if COMPILEERROR -#if !NET6_0_OR_GREATER - //ERROR: You cannot assign lambda - //expression to an implicitly - //typed local variable prior C#10 - var v = (int x) => x; -#endif -//#endif // COMPILEERROR + //#if COMPILEERROR + #if !NET6_0_OR_GREATER + //ERROR: Operator "." cannot be applied to + //operand of type "lambda expression" + string s = ((int x) => x).ToString(); + #endif + //#endif // COMPILEERROR } } diff --git a/src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs index c27ac3e8b..5b9a0abea 100644 --- a/src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs @@ -6,24 +6,14 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 5. - static public void JumpStatementsToOutOfScopeDestinations() + static public void PatternMatchingOnType() { //#if COMPILEERROR #if !NET6_0_OR_GREATER - //ERROR: Control cannot leave the body of an - //anonymous method or lambda expression - string[] args = {"/File", "fileThatMostCertainlyDoesNotExist"}; - Func f; - switch(args[0]) - { - case "/File": - f = () => - { - if (!File.Exists(args[1])) - break; - return args[1]; - }; - } + //ERROR: The first operand of an "is" or "as" + //operator may not be a lambda expression or + //anonymous method + bool b = ((int x) => x) is Func; #endif //#endif // COMPILEERROR } diff --git a/src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs index f3ba95308..7547f2e43 100644 --- a/src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs @@ -6,15 +6,13 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 6. - static public void AccessingParametersAndLocalsOutOfBody() + static public void ConvertingToImproperDelegate() { //#if COMPILEERROR #if !NET6_0_OR_GREATER - //ERROR: The name "first" does not - //exist in the current context - Func expression = - (first, second) => first > second; - first++; + //ERROR: Lambda expression is not compatible + //with Func type + Func f = (int x) => x; #endif //#endif // COMPILEERROR } diff --git a/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs index 02ef83033..932b6823d 100644 --- a/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs @@ -6,18 +6,24 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 7. - static public void UsingOutParameters() + static public void JumpStatementsToOutOfScopeDestinations() { //#if COMPILEERROR #if !NET6_0_OR_GREATER - int number; - Func f = - text => int.TryParse(text, out number); - if (f("1")) - { - //ERROR: Use of unassigned local variable - System.Console.Write(number); - } + //ERROR: Control cannot leave the body of an + //anonymous method or lambda expression + string[] args = {"/File", "fileThatMostCertainlyDoesNotExist"}; + Func f; + switch(args[0]) + { + case "/File": + f = () => + { + if (!File.Exists(args[1])) + break; + return args[1]; + }; + } #endif //#endif // COMPILEERROR } diff --git a/src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs index e7ee77ac3..ce168a92f 100644 --- a/src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs @@ -6,18 +6,15 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 8. - static public void CompilerWillNotDetectInLambdaAssignment() + static public void AccessingParametersAndLocalsOutOfBody() { //#if COMPILEERROR #if !NET6_0_OR_GREATER - int number; - Func isFortyTwo = - x => 42 == (number = x); - if(isFortyTwo(42)) - { - //ERROR: Use of unassigned local variable - System.Console.Write(number); - } + //ERROR: The name "first" does not + //exist in the current context + Func expression = + (first, second) => first > second; + first++; #endif //#endif // COMPILEERROR } diff --git a/src/Chapter13/Table13.01.i.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.i.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..7f43cf5c3 --- /dev/null +++ b/src/Chapter13/Table13.01.i.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,24 @@ +// Justification: Only snippets of source code shown for elucidation. +#pragma warning disable CS0168 // Variable is declared but never used + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; + +public partial class LambdaExpressionNotesAndExamples +{ + // 9. + static public void UsingOutParameters() + { +//#if COMPILEERROR +#if !NET6_0_OR_GREATER + int number; + Func f = + text => int.TryParse(text, out number); + if (f("1")) + { + //ERROR: Use of unassigned local variable + System.Console.Write(number); + } +#endif +//#endif // COMPILEERROR + } +} diff --git a/src/Chapter13/Table13.01.j.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.j.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..79c33a677 --- /dev/null +++ b/src/Chapter13/Table13.01.j.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,24 @@ +// Justification: Only snippets of source code shown for elucidation. +#pragma warning disable CS0168 // Variable is declared but never used + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; + +public partial class LambdaExpressionNotesAndExamples +{ + // 10. + static public void CompilerWillNotDetectInLambdaAssignment() + { +//#if COMPILEERROR +#if !NET6_0_OR_GREATER + int number; + Func isFortyTwo = + x => 42 == (number = x); + if(isFortyTwo(42)) + { + //ERROR: Use of unassigned local variable + System.Console.Write(number); + } +#endif +//#endif // COMPILEERROR + } +} From 8c0ca7e88927d4254ed2c5c40e66265d1df2cce2 Mon Sep 17 00:00:00 2001 From: A-Tanner Date: Tue, 19 Sep 2023 17:27:53 -0700 Subject: [PATCH 10/15] refactor: rewrite listing tests for table 13.01 --- src/Chapter13.Tests/Table13.01.Tests.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Chapter13.Tests/Table13.01.Tests.cs b/src/Chapter13.Tests/Table13.01.Tests.cs index 5ebfc0c91..fc9e80231 100644 --- a/src/Chapter13.Tests/Table13.01.Tests.cs +++ b/src/Chapter13.Tests/Table13.01.Tests.cs @@ -12,13 +12,13 @@ public class LambdaHighlightTests /* 1. */ [DataRow(".a")] /* 2. */ [DataRow(".b")] /* 3. */ [DataRow(".c")] - /* 4. */ [DataRow(".d")] - /* 5. */ [DataRow(".e")] - /* 6. */ [DataRow(".f")] - /* 7. */ [DataRow(".g")] - /* 8. */ [DataRow(".h")] - /* 9. */ [DataRow(".i")] - /* 10. */ [DataRow(".j")] + /* 4. */ [DataRow(".d", "CS0023")] + /* 5. */ [DataRow(".e", "CS0837")] + /* 6. */ [DataRow(".f", "CS0029", "CS1662")] + /* 7. */ [DataRow(".g", "CS8070", "CS1632")] + /* 8. */ [DataRow(".h", "CS0103")] + /* 9. */ [DataRow(".i", "CS0165")] + /* 10. */ [DataRow(".j", "CS0165")] public async Task ParseAndCompile(string fileNameSuffix, params string[] errorIds) { await CompilerAssert.CompileAsync($"Table13.01{fileNameSuffix}.LambdaExpressionNotesAndExamples.cs", errorIds); From 2a1056db5305caf3d73eab0409f68efbe1133c1f Mon Sep 17 00:00:00 2001 From: Mark Michaelis Date: Wed, 20 Sep 2023 12:56:37 +0100 Subject: [PATCH 11/15] Removed extra curly brace --- .../Table13.01.c.LambdaExpressionNotesAndExamples.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs index 80c551caf..e7d2c68b1 100644 --- a/src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs @@ -10,8 +10,8 @@ static public void ExpressionsCanHaveReturnTypes() { #if !NET6_0_OR_GREATER Action action = void () => { }; - var func = short?(long number) => number <= short.MaxValue ? (short)number : null; - } + var func = short?(long number) => + number <= short.MaxValue ? (short)number : null; #endif } } From 1671529927883dfc34dcb667a337d615feeb809d Mon Sep 17 00:00:00 2001 From: A-Tanner Date: Wed, 20 Sep 2023 10:15:06 -0700 Subject: [PATCH 12/15] fix: remove lang version from Chapter13.csproj --- src/Chapter13/Chapter13.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Chapter13/Chapter13.csproj b/src/Chapter13/Chapter13.csproj index f04af9c29..ac9e60305 100644 --- a/src/Chapter13/Chapter13.csproj +++ b/src/Chapter13/Chapter13.csproj @@ -1,7 +1,6 @@  13 - 10.0 From 917a511d3c1440236a519b719c8ded8fd91bf7fd Mon Sep 17 00:00:00 2001 From: Benjamin Michaelis Date: Wed, 20 Sep 2023 22:43:01 +0100 Subject: [PATCH 13/15] Apply suggestions from code review Co-authored-by: Kevin B --- src/Chapter13.Tests/Table13.01.Tests.cs | 1 - .../Table13.01.a.LambdaExpressionNotesAndExamples.cs | 4 ++-- .../Table13.01.d.LambdaExpressionNotesAndExamples.cs | 2 +- .../Table13.01.e.LambdaExpressionNotesAndExamples.cs | 2 +- .../Table13.01.f.LambdaExpressionNotesAndExamples.cs | 2 +- .../Table13.01.g.LambdaExpressionNotesAndExamples.cs | 2 +- .../Table13.01.h.LambdaExpressionNotesAndExamples.cs | 2 +- .../Table13.01.i.LambdaExpressionNotesAndExamples.cs | 2 +- .../Table13.01.j.LambdaExpressionNotesAndExamples.cs | 5 ++--- 9 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/Chapter13.Tests/Table13.01.Tests.cs b/src/Chapter13.Tests/Table13.01.Tests.cs index fc9e80231..966f4571e 100644 --- a/src/Chapter13.Tests/Table13.01.Tests.cs +++ b/src/Chapter13.Tests/Table13.01.Tests.cs @@ -6,7 +6,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01.Tests; [TestClass] public class LambdaHighlightTests { - public TestContext TestContext { get; set; } = null!; // Set by MSTest; [TestMethod] /* 1. */ [DataRow(".a")] diff --git a/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs index eeb63f277..8c8839162 100644 --- a/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs @@ -6,10 +6,10 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 1. - static public void DiscardParameters() + public static void DiscardParameters() { #if !NET6_0_OR_GREATER - Action x = (_,_)=> + Action x = (_, _)=> Console.WriteLine("This is a test."); #endif } diff --git a/src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs index bce9b1e33..fdd4d8b97 100644 --- a/src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs @@ -6,7 +6,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 4. - static public void MemberMethodsOnExpressions() + public static void MemberMethodsOnExpressions() { //#if COMPILEERROR #if !NET6_0_OR_GREATER diff --git a/src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs index 5b9a0abea..f6c11a055 100644 --- a/src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.e.LambdaExpressionNotesAndExamples.cs @@ -6,7 +6,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 5. - static public void PatternMatchingOnType() + public static void PatternMatchingOnType() { //#if COMPILEERROR #if !NET6_0_OR_GREATER diff --git a/src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs index 7547f2e43..77e5948de 100644 --- a/src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs @@ -6,7 +6,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 6. - static public void ConvertingToImproperDelegate() + public static void ConvertingToImproperDelegate() { //#if COMPILEERROR #if !NET6_0_OR_GREATER diff --git a/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs index 932b6823d..c2aae6a60 100644 --- a/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs @@ -6,7 +6,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 7. - static public void JumpStatementsToOutOfScopeDestinations() + public static void JumpStatementsToOutOfScopeDestinations() { //#if COMPILEERROR #if !NET6_0_OR_GREATER diff --git a/src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs index ce168a92f..ff6999e86 100644 --- a/src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs @@ -6,7 +6,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 8. - static public void AccessingParametersAndLocalsOutOfBody() + public static void AccessingParametersAndLocalsOutOfBody() { //#if COMPILEERROR #if !NET6_0_OR_GREATER diff --git a/src/Chapter13/Table13.01.i.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.i.LambdaExpressionNotesAndExamples.cs index 7f43cf5c3..0e3466518 100644 --- a/src/Chapter13/Table13.01.i.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.i.LambdaExpressionNotesAndExamples.cs @@ -6,7 +6,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 9. - static public void UsingOutParameters() + public static void UsingOutParameters() { //#if COMPILEERROR #if !NET6_0_OR_GREATER diff --git a/src/Chapter13/Table13.01.j.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.j.LambdaExpressionNotesAndExamples.cs index 79c33a677..7a34da2a9 100644 --- a/src/Chapter13/Table13.01.j.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.j.LambdaExpressionNotesAndExamples.cs @@ -6,13 +6,12 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01; public partial class LambdaExpressionNotesAndExamples { // 10. - static public void CompilerWillNotDetectInLambdaAssignment() + public static void CompilerWillNotDetectInLambdaAssignment() { //#if COMPILEERROR #if !NET6_0_OR_GREATER - int number; Func isFortyTwo = - x => 42 == (number = x); + x => 42 == x; if(isFortyTwo(42)) { //ERROR: Use of unassigned local variable From c10ee1934470a4f35917eb8ee66f226bbe913154 Mon Sep 17 00:00:00 2001 From: Benjamin Michaelis Date: Wed, 20 Sep 2023 22:47:15 +0100 Subject: [PATCH 14/15] Cleanup --- ...3.01.g.LambdaExpressionNotesAndExamples.cs | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs index c2aae6a60..6818489c1 100644 --- a/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs @@ -8,23 +8,23 @@ public partial class LambdaExpressionNotesAndExamples // 7. public static void JumpStatementsToOutOfScopeDestinations() { -//#if COMPILEERROR + //#if COMPILEERROR #if !NET6_0_OR_GREATER //ERROR: Control cannot leave the body of an //anonymous method or lambda expression - string[] args = {"/File", "fileThatMostCertainlyDoesNotExist"}; - Func f; - switch(args[0]) - { - case "/File": - f = () => - { - if (!File.Exists(args[1])) - break; - return args[1]; - }; - } + string[] args = { "/File", "fileThatMostCertainlyDoesNotExist" }; + Func f; + switch (args[0]) + { + case "/File": + f = () => + { + if (!File.Exists(args[1])) + break; + return args[1]; + }; + } #endif -//#endif // COMPILEERROR + //#endif // COMPILEERROR } } From a87cadaf8b9646af25bf747d8f0e1a43cd9be601 Mon Sep 17 00:00:00 2001 From: Benjamin Michaelis Date: Wed, 20 Sep 2023 23:25:39 +0100 Subject: [PATCH 15/15] Cleanup and fix --- ...3.01.j.LambdaExpressionNotesAndExamples.cs | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Chapter13/Table13.01.j.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.j.LambdaExpressionNotesAndExamples.cs index 7a34da2a9..245576ca2 100644 --- a/src/Chapter13/Table13.01.j.LambdaExpressionNotesAndExamples.cs +++ b/src/Chapter13/Table13.01.j.LambdaExpressionNotesAndExamples.cs @@ -8,16 +8,18 @@ public partial class LambdaExpressionNotesAndExamples // 10. public static void CompilerWillNotDetectInLambdaAssignment() { -//#if COMPILEERROR + //#if COMPILEERROR #if !NET6_0_OR_GREATER - Func isFortyTwo = - x => 42 == x; - if(isFortyTwo(42)) - { - //ERROR: Use of unassigned local variable - System.Console.Write(number); - } + + int number; + Func isFortyTwo = + x => 42 == (number = x); + if (isFortyTwo(42)) + { + // ERROR: Use of unassigned local variable + System.Console.Write(number); + } #endif -//#endif // COMPILEERROR + //#endif // COMPILEERROR } }