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