From cc041b86e44de43a2b64a7bc30e822743e907877 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 11 Sep 2023 08:57:56 -0700 Subject: [PATCH 1/2] fix: Uncommented listing and added compilerAssert tests Had to add an extra class that derived from PdaItem. The code now throws the error --- src/Chapter12.Tests/Listing12.41.Tests.cs | 15 +++++++++++++++ ...1.PreventingCovarianceMaintainsHomogeneity.cs | 16 +++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 src/Chapter12.Tests/Listing12.41.Tests.cs diff --git a/src/Chapter12.Tests/Listing12.41.Tests.cs b/src/Chapter12.Tests/Listing12.41.Tests.cs new file mode 100644 index 000000000..85a1698bb --- /dev/null +++ b/src/Chapter12.Tests/Listing12.41.Tests.cs @@ -0,0 +1,15 @@ +using AddisonWesley.Michaelis.EssentialCSharp.Shared.Tests; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_41.Tests; + +[TestClass] +public class Listing12 +{ + [TestMethod] + public async Task CannotConvertTypes() + { + await CompilerAssert.CompileTestTargetFileAsync( + new string[] { "CS0030" }); + } +} diff --git a/src/Chapter12/Listing12.41.PreventingCovarianceMaintainsHomogeneity.cs b/src/Chapter12/Listing12.41.PreventingCovarianceMaintainsHomogeneity.cs index 2d5678588..48354b5ea 100644 --- a/src/Chapter12/Listing12.41.PreventingCovarianceMaintainsHomogeneity.cs +++ b/src/Chapter12/Listing12.41.PreventingCovarianceMaintainsHomogeneity.cs @@ -1,4 +1,5 @@ -using AddisonWesley.Michaelis.EssentialCSharp.Chapter08.Listing08_05; +using AddisonWesley.Michaelis.EssentialCSharp.Chapter08.Listing08_02; +using AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_08; using AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_11; namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_41; @@ -14,12 +15,21 @@ public static void Main() Pair contacts = new(contact1, contact2); #region HIGHLIGHT + #if COMPILEERROR // EXCLUDE // This gives an error: Cannot convert type ... // But suppose it did not - // IPair pdaPair = (IPair) contacts; + IPair pdaPair = (IPair) contacts; // This is perfectly legal, but not type-safe - // pair.First = new Address("123 Sesame Street"); + pdaPair.First = new Address("123 Sesame Street"); + #endif // COMPILEERROR // EXCLUDE #endregion HIGHLIGHT #endregion INCLUDE } } + +public class Address : PdaItem +{ + public Address(string address) : base(address) + { + } +} From 82e75bb3b7f2d20e8ec451bc23402cf64e5c2f5e Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 11 Sep 2023 09:34:07 -0700 Subject: [PATCH 2/2] fix: Had to add classes inline to bypass other errors --- ...reventingCovarianceMaintainsHomogeneity.cs | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/Chapter12/Listing12.41.PreventingCovarianceMaintainsHomogeneity.cs b/src/Chapter12/Listing12.41.PreventingCovarianceMaintainsHomogeneity.cs index 48354b5ea..83f996371 100644 --- a/src/Chapter12/Listing12.41.PreventingCovarianceMaintainsHomogeneity.cs +++ b/src/Chapter12/Listing12.41.PreventingCovarianceMaintainsHomogeneity.cs @@ -1,7 +1,3 @@ -using AddisonWesley.Michaelis.EssentialCSharp.Chapter08.Listing08_02; -using AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_08; -using AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_11; - namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter12.Listing12_41; public class Program @@ -33,3 +29,39 @@ public Address(string address) : base(address) { } } + +public struct Pair : IPair +{ + public Pair(T first, T second) + { + First = first; + Second = second; + } + + public T First { get; set; } + public T Second { get; set; } +} + +interface IPair +{ + T First { get; set; } + T Second { get; set; } +} + +public class Contact : PdaItem +{ + public Contact(string name) + : base(name) + { + } +} + +public abstract class PdaItem +{ + public PdaItem(string name) + { + Name = name; + } + + public virtual string Name { get; set; } +}