From 6d888fff031ccb840f7f4aedf7b8002e16126121 Mon Sep 17 00:00:00 2001 From: Mark Michaelis Date: Wed, 20 Sep 2023 18:02:57 +0100 Subject: [PATCH 1/3] Temporarily disable select Chapter 20 tests (#550) IntelliTect.com is failing so tests are failing. Temporarily disabled the failing tests. --- src/Chapter20.Tests/BaseProgramTests.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Chapter20.Tests/BaseProgramTests.cs b/src/Chapter20.Tests/BaseProgramTests.cs index 2df88254f..f309500db 100644 --- a/src/Chapter20.Tests/BaseProgramTests.cs +++ b/src/Chapter20.Tests/BaseProgramTests.cs @@ -27,6 +27,7 @@ public void TestInitialize() } [TestMethod] + [Ignore] [DataRow("IntelliTect", @"[1-9]\d*")] [DataRow("Text Snippet That Does Not Exist On The Page", @"0")] public void Main_FindText_VerifyOccurenceCount(string findText, string countPattern) @@ -106,7 +107,7 @@ protected static void AssertAggregateExceptionType(string messagePrefix, Aggrega ExceptionDispatchInfo.Capture( innerException!).Throw(); - return true; // Identifhy whether the exception should report that it is handled or not. + return true; // Identify whether the exception should report that it is handled or not. }); } @@ -168,7 +169,7 @@ public async Task Main_GivenBadUri_ThrowException(string uri, string defaultMess } if (exception is AggregateException aggregateException) { - // Innerexception expected to be WebException. + // InnerException expected to be WebException. exception = aggregateException.Flatten(); aggregateException.Handle(innerException => { From 65ff6554621c7bbfe04eca8f1e70724194b4a32d Mon Sep 17 00:00:00 2001 From: Mark Michaelis Date: Wed, 20 Sep 2023 22:26:30 +0100 Subject: [PATCH 2/3] Add code for Generic attributes and Caller* attributes - Chapter 18 (#548) --- .../Listing06.27.CallingAConstructor.cs | 2 +- .../Listing18.26.GenericAttributes.Tests.cs | 24 ++++++++ ...ing18.27.CallerArgumentExpression.Tests.cs | 61 +++++++++++++++++++ ...ynamicProgrammingUsingReflection.Tests.cs} | 6 +- ...ndingToXMLElementsWithoutDynamic.Tests.cs} | 6 +- ....RuntimeBindingToXMLWithDynamics.Tests.cs} | 6 +- src/Chapter18/Chapter18.csproj | 4 +- .../Listing18.26.GenericAttributes.cs | 44 +++++++++++++ .../Listing18.27.CallerArgumentExpression.cs | 59 ++++++++++++++++++ ...8.28.DynamicProgrammingUsingReflection.cs} | 3 +- ...timeBindingToXMLElementsWithoutDynamic.cs} | 2 +- ...g18.30.RuntimeBindingToXMLWithDynamics.cs} | 4 +- ...18.31.ImplementingACustomDynamicObject.cs} | 2 +- ...eMembersOnSystem.Dynamic.DynamicObject.cs} | 2 +- 14 files changed, 207 insertions(+), 18 deletions(-) create mode 100644 src/Chapter18.Tests/Listing18.26.GenericAttributes.Tests.cs create mode 100644 src/Chapter18.Tests/Listing18.27.CallerArgumentExpression.Tests.cs rename src/Chapter18.Tests/{Listing18.26.Tests.cs => Listing18.28.DynamicProgrammingUsingReflection.Tests.cs} (83%) rename src/Chapter18.Tests/{Listing18.27.Tests.cs => Listing18.29.RuntimeBindingToXMLElementsWithoutDynamic.Tests.cs} (81%) rename src/Chapter18.Tests/{Listing18.28.Tests.cs => Listing18.30.RuntimeBindingToXMLWithDynamics.Tests.cs} (81%) create mode 100644 src/Chapter18/Listing18.26.GenericAttributes.cs create mode 100644 src/Chapter18/Listing18.27.CallerArgumentExpression.cs rename src/Chapter18/{Listing18.26.DynamicProgrammingUsingReflection.cs => Listing18.28.DynamicProgrammingUsingReflection.cs} (96%) rename src/Chapter18/{Listing18.27.RuntimeBindingToXMLElementsWithoutDynamic.cs => Listing18.29.RuntimeBindingToXMLElementsWithoutDynamic.cs} (98%) rename src/Chapter18/{Listing18.28.RuntimeBindingToXMLWithDynamics.cs => Listing18.30.RuntimeBindingToXMLWithDynamics.cs} (93%) rename src/Chapter18/{Listing18.29.ImplementingACustomDynamicObject.cs => Listing18.31.ImplementingACustomDynamicObject.cs} (99%) rename src/Chapter18/{Listing18.30.OverridableMembersOnSystem.Dynamic.DynamicObject.cs => Listing18.32.OverridableMembersOnSystem.Dynamic.DynamicObject.cs} (95%) diff --git a/src/Chapter06/Listing06.27.CallingAConstructor.cs b/src/Chapter06/Listing06.27.CallingAConstructor.cs index 6608fa1ec..8fcf2defc 100644 --- a/src/Chapter06/Listing06.27.CallingAConstructor.cs +++ b/src/Chapter06/Listing06.27.CallingAConstructor.cs @@ -1,6 +1,6 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter06.Listing06_27; -using Listing06_28; +using Listing06_30; #region INCLUDE public class Program diff --git a/src/Chapter18.Tests/Listing18.26.GenericAttributes.Tests.cs b/src/Chapter18.Tests/Listing18.26.GenericAttributes.Tests.cs new file mode 100644 index 000000000..902709e4c --- /dev/null +++ b/src/Chapter18.Tests/Listing18.26.GenericAttributes.Tests.cs @@ -0,0 +1,24 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_26.Tests; + +#if NET7_0_OR_GREATER +[TestClass] +public class GenericExceptionTests +{ + [TestMethod] + public void ExpectedExceptionIsThrown() + { + ExpectedException.AssertExceptionThrown( + SampleTests.ThrowDivideByZeroExceptionTest); + } + + [TestMethod] + [ExpectedException(typeof(InvalidOperationException))] + public void ExpectedExceptionIsNotThrown() + { + ExpectedException.AssertExceptionThrown( + () => { }); + } +} +#endif // NET7_0_OR_GREATER diff --git a/src/Chapter18.Tests/Listing18.27.CallerArgumentExpression.Tests.cs b/src/Chapter18.Tests/Listing18.27.CallerArgumentExpression.Tests.cs new file mode 100644 index 000000000..e919f6c96 --- /dev/null +++ b/src/Chapter18.Tests/Listing18.27.CallerArgumentExpression.Tests.cs @@ -0,0 +1,61 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_27.Tests; + +#if NET7_0_OR_GREATER +[TestClass] +public class GenericExceptionTests +{ + [TestMethod] + public void ExpectedExceptionIsThrown() + { + ExpectedException.AssertExceptionThrown( + SampleTests.ThrowArgumentNullExceptionTest); + } + + [TestMethod] + public void VerifyExpectedExceptionMessage() + { + try + { + ExpectedException.AssertExceptionThrown( + () => { }); + } + catch(InvalidOperationException exception) + { + Assert.IsTrue(exception.Message.Contains("'() => { }'")); + } + } + + [TestMethod] + public void Method() + { + try + { + SampleTests.Method(); + } + catch (InvalidOperationException exception) + { + Assert.IsTrue( + exception.Message.Contains("'() => { }'") && + exception.Message.Contains("'Method'") && + exception.Message.Contains("'./FileName.cs'")); + // The expected exception, System.DivideByZeroException, + // was not thrown by the expression, 'Method' in the method, './FileName.cs', and file 'C:\Git\EssentialCSharp\src\Chapter18\Listing18.25b.CallerArgumentExpression.cs'. + } + } + + private object PassingMethodNameAndFileName() + { + throw new NotImplementedException(); + } + + [TestMethod] + [ExpectedException(typeof(InvalidOperationException))] + public void ExpectedExceptionIsNotThrown() + { + ExpectedException.AssertExceptionThrown( + () => { }); + } +} +#endif // NET7_0_OR_GREATER diff --git a/src/Chapter18.Tests/Listing18.26.Tests.cs b/src/Chapter18.Tests/Listing18.28.DynamicProgrammingUsingReflection.Tests.cs similarity index 83% rename from src/Chapter18.Tests/Listing18.26.Tests.cs rename to src/Chapter18.Tests/Listing18.28.DynamicProgrammingUsingReflection.Tests.cs index c69f6f1eb..9a866d020 100644 --- a/src/Chapter18.Tests/Listing18.26.Tests.cs +++ b/src/Chapter18.Tests/Listing18.28.DynamicProgrammingUsingReflection.Tests.cs @@ -1,6 +1,6 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_26.Tests; +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_28.Tests; [TestClass] public class ProgramTests @@ -14,4 +14,4 @@ public void MainTest() IntelliTect.TestTools.Console.ConsoleAssert.Expect( expected, Program.Main); } -} \ No newline at end of file +} diff --git a/src/Chapter18.Tests/Listing18.27.Tests.cs b/src/Chapter18.Tests/Listing18.29.RuntimeBindingToXMLElementsWithoutDynamic.Tests.cs similarity index 81% rename from src/Chapter18.Tests/Listing18.27.Tests.cs rename to src/Chapter18.Tests/Listing18.29.RuntimeBindingToXMLElementsWithoutDynamic.Tests.cs index 43f414d43..8316cfc9d 100644 --- a/src/Chapter18.Tests/Listing18.27.Tests.cs +++ b/src/Chapter18.Tests/Listing18.29.RuntimeBindingToXMLElementsWithoutDynamic.Tests.cs @@ -1,6 +1,6 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_27.Tests; +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_29.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/Chapter18.Tests/Listing18.28.Tests.cs b/src/Chapter18.Tests/Listing18.30.RuntimeBindingToXMLWithDynamics.Tests.cs similarity index 81% rename from src/Chapter18.Tests/Listing18.28.Tests.cs rename to src/Chapter18.Tests/Listing18.30.RuntimeBindingToXMLWithDynamics.Tests.cs index 344a7b5e1..90b2e1786 100644 --- a/src/Chapter18.Tests/Listing18.28.Tests.cs +++ b/src/Chapter18.Tests/Listing18.30.RuntimeBindingToXMLWithDynamics.Tests.cs @@ -1,6 +1,6 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_28.Tests; +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_30.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/Chapter18/Chapter18.csproj b/src/Chapter18/Chapter18.csproj index 182e79bb1..f0dfe4c1f 100644 --- a/src/Chapter18/Chapter18.csproj +++ b/src/Chapter18/Chapter18.csproj @@ -15,7 +15,7 @@ - + Program.cs @@ -29,6 +29,6 @@ DoWorkEventArgs.cs - + diff --git a/src/Chapter18/Listing18.26.GenericAttributes.cs b/src/Chapter18/Listing18.26.GenericAttributes.cs new file mode 100644 index 000000000..fb5a72ef5 --- /dev/null +++ b/src/Chapter18/Listing18.26.GenericAttributes.cs @@ -0,0 +1,44 @@ +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_26; + +#if NET7_0_OR_GREATER +#region INCLUDE +public class SampleTests +{ + #region HIGHLIGHT + [ExpectedException] + #endregion HIGHLIGHT + public static void ThrowDivideByZeroExceptionTest() + { + var result = 1/"".Length; + } +} + +[AttributeUsage(AttributeTargets.Method)] +#region HIGHLIGHT +public class ExpectedException : + Attribute where TException : Exception +#endregion HIGHLIGHT +{ + #region EXCLUDE + public static TException AssertExceptionThrown(Action testMethod) + { + try + { + testMethod(); + throw new InvalidOperationException( + $"The expected exception, { + typeof(TException).FullName }, was not thrown."); + } + catch (TException exception) + { + return exception; + } + } + + // Attribute detection + #endregion EXCLUDE + + // ... +} +#endregion INCLUDE +#endif // NET7_0_OR_GREATER diff --git a/src/Chapter18/Listing18.27.CallerArgumentExpression.cs b/src/Chapter18/Listing18.27.CallerArgumentExpression.cs new file mode 100644 index 000000000..b0e255beb --- /dev/null +++ b/src/Chapter18/Listing18.27.CallerArgumentExpression.cs @@ -0,0 +1,59 @@ +using System.Runtime.CompilerServices; + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_27; + +#if NET7_0_OR_GREATER +#region INCLUDE +public class SampleTests +{ + #region HIGHLIGHT + [ExpectedException] + #endregion HIGHLIGHT + public static void ThrowArgumentNullExceptionTest() + { + var result = 1/"".Length; + } + +public static void Method() +{ + ExpectedException.AssertExceptionThrown( + () => throw new DivideByZeroException()); +} +} + +[AttributeUsage(AttributeTargets.Method)] +public class ExpectedException : + Attribute where TException : Exception +{ + #region HIGHLIGHT + public static TException AssertExceptionThrown( + Action testAction, + [CallerArgumentExpression(nameof(testAction))] + string testExpression = null!, + [CallerMemberName]string testActionMemberName = null!, + [CallerFilePath]string testActionFileName = null! + ) + #endregion HIGHLIGHT + { + try + { + testAction(); + throw new InvalidOperationException( + $"The expected exception, { + typeof(TException).FullName }, was not thrown" + + $" by the expression '{ + testExpression }' in the method '{ + testActionMemberName }' and file '{ + testActionFileName }'."); + } + catch (TException exception) + { + return exception; + } + } + + // Attribute detection + // ... +} +#endregion INCLUDE +#endif // NET7_0_OR_GREATER diff --git a/src/Chapter18/Listing18.26.DynamicProgrammingUsingReflection.cs b/src/Chapter18/Listing18.28.DynamicProgrammingUsingReflection.cs similarity index 96% rename from src/Chapter18/Listing18.26.DynamicProgrammingUsingReflection.cs rename to src/Chapter18/Listing18.28.DynamicProgrammingUsingReflection.cs index 1d52070c9..1d54f7076 100644 --- a/src/Chapter18/Listing18.26.DynamicProgrammingUsingReflection.cs +++ b/src/Chapter18/Listing18.28.DynamicProgrammingUsingReflection.cs @@ -1,10 +1,11 @@ -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_26; +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_28; public class Program { public static void Main() { #region INCLUDE + // ... dynamic data = "Hello! My name is Inigo Montoya"; Console.WriteLine(data); diff --git a/src/Chapter18/Listing18.27.RuntimeBindingToXMLElementsWithoutDynamic.cs b/src/Chapter18/Listing18.29.RuntimeBindingToXMLElementsWithoutDynamic.cs similarity index 98% rename from src/Chapter18/Listing18.27.RuntimeBindingToXMLElementsWithoutDynamic.cs rename to src/Chapter18/Listing18.29.RuntimeBindingToXMLElementsWithoutDynamic.cs index 7af50d29e..2a87e91e8 100644 --- a/src/Chapter18/Listing18.27.RuntimeBindingToXMLElementsWithoutDynamic.cs +++ b/src/Chapter18/Listing18.29.RuntimeBindingToXMLElementsWithoutDynamic.cs @@ -1,4 +1,4 @@ -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_27; +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_29; #region INCLUDE using System.Xml.Linq; diff --git a/src/Chapter18/Listing18.28.RuntimeBindingToXMLWithDynamics.cs b/src/Chapter18/Listing18.30.RuntimeBindingToXMLWithDynamics.cs similarity index 93% rename from src/Chapter18/Listing18.28.RuntimeBindingToXMLWithDynamics.cs rename to src/Chapter18/Listing18.30.RuntimeBindingToXMLWithDynamics.cs index 6ae9cd733..a5ad68082 100644 --- a/src/Chapter18/Listing18.28.RuntimeBindingToXMLWithDynamics.cs +++ b/src/Chapter18/Listing18.30.RuntimeBindingToXMLWithDynamics.cs @@ -1,6 +1,6 @@ -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_28; +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_30; -using Listing18_29; +using Listing18_31; public class Program { public static void Main() diff --git a/src/Chapter18/Listing18.29.ImplementingACustomDynamicObject.cs b/src/Chapter18/Listing18.31.ImplementingACustomDynamicObject.cs similarity index 99% rename from src/Chapter18/Listing18.29.ImplementingACustomDynamicObject.cs rename to src/Chapter18/Listing18.31.ImplementingACustomDynamicObject.cs index 61d6ba8ed..c02a24c57 100644 --- a/src/Chapter18/Listing18.29.ImplementingACustomDynamicObject.cs +++ b/src/Chapter18/Listing18.31.ImplementingACustomDynamicObject.cs @@ -1,4 +1,4 @@ -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_29; +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_31; #region INCLUDE using System.Dynamic; diff --git a/src/Chapter18/Listing18.30.OverridableMembersOnSystem.Dynamic.DynamicObject.cs b/src/Chapter18/Listing18.32.OverridableMembersOnSystem.Dynamic.DynamicObject.cs similarity index 95% rename from src/Chapter18/Listing18.30.OverridableMembersOnSystem.Dynamic.DynamicObject.cs rename to src/Chapter18/Listing18.32.OverridableMembersOnSystem.Dynamic.DynamicObject.cs index bdf90872d..46b23bb92 100644 --- a/src/Chapter18/Listing18.30.OverridableMembersOnSystem.Dynamic.DynamicObject.cs +++ b/src/Chapter18/Listing18.32.OverridableMembersOnSystem.Dynamic.DynamicObject.cs @@ -1,4 +1,4 @@ -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_30 +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_32 { #region INCLUDE using System.Collections.Generic; From 74939e63860c0839c1760eb94cd692d8b4b078c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anr=C3=A9=20=22Ray=22=20Tanner?= <40047765+A-Tanner@users.noreply.github.com> Date: Wed, 20 Sep 2023 20:36:35 -0700 Subject: [PATCH 3/3] Mmichaelis/updating tables (#547) Adds code listings and tests for Table 13.01 Note : test c does not generate any errors as of version 10.0, and the content should be edited to reflect this Fixes [#1529](https://github.com/IntelliTect-dev/EssentialCSharp.Tooling/issues/1529) --------- Co-authored-by: Mark Michaelis --- src/Chapter13.Tests/Table13.01.Tests.cs | 25 ++++++++++++++++ ...3.01.a.LambdaExpressionNotesAndExamples.cs | 16 ++++++++++ ...3.01.b.LambdaExpressionNotesAndExamples.cs | 18 +++++++++++ ...3.01.c.LambdaExpressionNotesAndExamples.cs | 17 +++++++++++ ...3.01.d.LambdaExpressionNotesAndExamples.cs | 19 ++++++++++++ ...3.01.e.LambdaExpressionNotesAndExamples.cs | 20 +++++++++++++ ...3.01.f.LambdaExpressionNotesAndExamples.cs | 19 ++++++++++++ ...3.01.g.LambdaExpressionNotesAndExamples.cs | 30 +++++++++++++++++++ ...3.01.h.LambdaExpressionNotesAndExamples.cs | 21 +++++++++++++ ...3.01.i.LambdaExpressionNotesAndExamples.cs | 24 +++++++++++++++ ...3.01.j.LambdaExpressionNotesAndExamples.cs | 25 ++++++++++++++++ 11 files changed, 234 insertions(+) create mode 100644 src/Chapter13.Tests/Table13.01.Tests.cs create mode 100644 src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.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 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 new file mode 100644 index 000000000..966f4571e --- /dev/null +++ b/src/Chapter13.Tests/Table13.01.Tests.cs @@ -0,0 +1,25 @@ +using AddisonWesley.Michaelis.EssentialCSharp.Shared.Tests; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Table13_01.Tests; + +[TestClass] +public class LambdaHighlightTests +{ + + [TestMethod] + /* 1. */ [DataRow(".a")] + /* 2. */ [DataRow(".b")] + /* 3. */ [DataRow(".c")] + /* 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); + } +} \ No newline at end of file diff --git a/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..8c8839162 --- /dev/null +++ b/src/Chapter13/Table13.01.a.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,16 @@ +// 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 +{ + // 1. + public static void DiscardParameters() + { +#if !NET6_0_OR_GREATER + 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 new file mode 100644 index 000000000..b9aaa69f6 --- /dev/null +++ b/src/Chapter13/Table13.01.b.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,18 @@ +// 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 TypeInferenceOfExpression() + { +#if !NET6_0_OR_GREATER + //You can assign lambda + //expression to an implicitly + //typed local variable starting C#10 + var v = (int x) => x; +#endif + } +} diff --git a/src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.c.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..e7d2c68b1 --- /dev/null +++ b/src/Chapter13/Table13.01.c.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 LambdaExpressionNotesAndExamples +{ + // 3. + static public void ExpressionsCanHaveReturnTypes() + { +#if !NET6_0_OR_GREATER + Action action = void () => { }; + var func = short?(long number) => + number <= short.MaxValue ? (short)number : null; +#endif + } +} diff --git a/src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.d.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..fdd4d8b97 --- /dev/null +++ b/src/Chapter13/Table13.01.d.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 +{ + // 4. + public static void MemberMethodsOnExpressions() + { + //#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 new file mode 100644 index 000000000..f6c11a055 --- /dev/null +++ b/src/Chapter13/Table13.01.e.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 +{ + // 5. + public static 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.f.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.f.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..77e5948de --- /dev/null +++ b/src/Chapter13/Table13.01.f.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 +{ + // 6. + public static 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.g.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.g.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..6818489c1 --- /dev/null +++ b/src/Chapter13/Table13.01.g.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 +{ + // 7. + public static 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.h.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.h.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..ff6999e86 --- /dev/null +++ b/src/Chapter13/Table13.01.h.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 +{ + // 8. + public static 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.i.LambdaExpressionNotesAndExamples.cs b/src/Chapter13/Table13.01.i.LambdaExpressionNotesAndExamples.cs new file mode 100644 index 000000000..0e3466518 --- /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. + public static 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..245576ca2 --- /dev/null +++ b/src/Chapter13/Table13.01.j.LambdaExpressionNotesAndExamples.cs @@ -0,0 +1,25 @@ +// 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. + public static 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 + } +}