Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Chapter07/Listing07.01.DerivingOneClassFromAnother.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
using System;
using System.Diagnostics.CodeAnalysis;

#region INCLUDE
public class PdaItem
{
[DisallowNull]
public string? Name { get; set; }
public DateTime LastUpdated { get; set; }
}

// Define the Contact class as inheriting the PdaItem class
#region HIGHLIGHT
public class Contact : PdaItem
#endregion
{
public string? Address { get; set; }
public string? Phone { get; set; }
}
#endregion
}
5 changes: 5 additions & 0 deletions src/Chapter07/Listing07.02.UsingInheritedMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
{
using Listing07_01;

#region INCLUDE
public class Program
{
public static void Main()
{
Contact contact = new Contact();
#region HIGHLIGHT
contact.Name = "Inigo Montoya";
#endregion

// ...
}
}
#endregion
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_03
{
#region INCLUDE
public class PdaItem : object
{
// ...
Expand All @@ -16,4 +17,5 @@ public class Customer : Contact
{
// ...
}
#endregion
}
6 changes: 6 additions & 0 deletions src/Chapter07/Listing07.04.ImplicitBaseTypeCasting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@
{
using Listing07_03;

#region INCLUDE
public class Program
{
public static void Main()
{
// Derived types can be implicitly converted to
// base types
Contact contact = new Contact();
#region HIGHLIGHT
PdaItem item = contact;
#endregion
// ...

// Base types must be cast explicitly to derived types
#region HIGHLIGHT
contact = (Contact)item;
#endregion
// ...
}
}
#endregion
}
5 changes: 4 additions & 1 deletion src/Chapter07/Listing07.05.DefiningCastOperators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_05
{
#region INCLUDE
class GPSCoordinates
{
// ...

public static implicit operator UTMCoordinates(
GPSCoordinates coordinates)
{
// ...
#region EXCLUDE
return null!; //return the new UTMCoordinates object
#endregion
}
}
#endregion

class UTMCoordinates
{
Expand Down
21 changes: 13 additions & 8 deletions src/Chapter07/Listing07.06.PrivateMembersAreNotInherited.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_06
{

#region INCLUDE
public class PdaItem
{
// Justification: Disabled pending constructor
#pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
#region EXCLUDE
// Justification: Disabled pending constructor
#pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
#endregion
private string _Name;
#pragma warning restore CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.

public string Name
{
get { return _Name; }
set { _Name = value; }
}

// ...
#region EXCLUDE
#pragma warning restore CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
#endregion
}

public class Contact : PdaItem
Expand All @@ -32,9 +34,12 @@ public class Program
public static void Main()
{
Contact contact = new Contact();

#region HIGHLIGHT
// ERROR: 'PdaItem._Name' is inaccessible
// contact._Name = "Inigo Montoya"; //uncomment this line and it will not compile
// due to its protection level
// contact._Name = "Inigo Montoya";
#endregion
}
}
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_07
{
using System;
#region INCLUDE
using System.IO;

public class PdaItem
{
public PdaItem(Guid objectKey) => ObjectKey = objectKey;
#region HIGHLIGHT
protected Guid ObjectKey { get; }
#endregion
}

public class Contact : PdaItem
Expand All @@ -21,17 +24,23 @@ public void Save()
{
// Instantiate a FileStream using <ObjectKey>.dat
// for the filename
#region HIGHLIGHT
using FileStream stream = File.OpenWrite(
ObjectKey + ".dat");
#endregion
// ...
stream.Dispose();
}
static public Contact Copy(Contact contact)
#region HIGHLIGHT
=> new Contact(contact.ObjectKey);
#endregion

// static public Contact Copy(PdaItem pdaItem) =>
// Error: Cannot access protected member PdaItem.ObjectKey.
// new Contact(((Contact)pdaItem).ObjectKey);
// static public Contact Copy(PdaItem pdaItem) =>
#region HIGHLIGHT
// Error: Cannot access protected member PdaItem.ObjectKey.
// new Contact(((Contact)pdaItem).ObjectKey);
#endregion
}

public class Program
Expand All @@ -40,8 +49,11 @@ public static void Main()
{
Contact contact = new Contact(Guid.NewGuid());

#region HIGHLIGHT
// ERROR: 'PdaItem.ObjectKey' is inaccessible
// Console.WriteLine(contact.ObjectKey);
#endregion
}
}
#endregion
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_08
{
#region INCLUDE
public sealed class CommandLineParser
{
// ...
}
// ERROR: Sealed classes cannot be derived from
public sealed class DerivedCommandLineParser
//: CommandLineParser //uncomment this line and it will not compile
//: CommandLineParser
{
// ...
}
#endregion
}
6 changes: 6 additions & 0 deletions src/Chapter07/Listing07.09.OverridingAProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_09
{
#region INCLUDE
public class PdaItem
{
#region HIGHLIGHT
public virtual string Name { get; set; }
#endregion
// ...
}

public class Contact : PdaItem
{
#region HIGHLIGHT
public override string Name
#endregion
{
get
{
Expand All @@ -32,4 +37,5 @@ public override string Name

// ...
}
#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_10
using System;
using Listing07_09;

#region INCLUDE
public class Program
{
public static void Main()
Expand All @@ -22,4 +23,5 @@ public static void Main()
$"{ contact.FirstName } { contact.LastName}");
}
}
#endregion
}
3 changes: 2 additions & 1 deletion src/Chapter07/Listing07.11.OverrideVersusNewModifier.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// TODO: Update listing in Manuscript
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_11
{
using System;

#region INCLUDE
public class Program
{
public class BaseClass
Expand Down Expand Up @@ -54,4 +54,5 @@ SuperSubDerivedClass superSubDerivedClass
BaseClass.DisplayName();
}
}
#endregion
}
2 changes: 2 additions & 0 deletions src/Chapter07/Listing07.12.SealingMembers.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_12
{
#region INCLUDE
class A
{
public virtual void Method()
Expand All @@ -21,4 +22,5 @@ class C : B
//{
//}
}
#endregion
}
13 changes: 4 additions & 9 deletions src/Chapter07/Listing07.13.AccessingABaseMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,11 @@

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_13
{
#region INCLUDE
using static System.Environment;

public class Address
{
//public Address(string streetAddress, string city,
// string state, string zip)
//{
// StreetAddress = streetAddress;
// City = city;
// State = state;
// Zip = zip;
//}

public string StreetAddress;
public string City;
public string State;
Expand All @@ -34,8 +26,11 @@ public class InternationalAddress : Address

public override string ToString()
{
#region HIGHLIGHT
return base.ToString() +
#endregion
NewLine + Country;
}
}
#endregion
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
using System;
using System.Diagnostics.CodeAnalysis;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_14
{
#region INCLUDE
public class PdaItem
{
public PdaItem(string name)
{
Name = name;
}

// ...

public virtual string Name { get; set; }
// ...
}
public class Contact : PdaItem
{
// Disable warning since FirstName&LastName set via Name property
#pragma warning disable CS8618 // Non-nullable field is uninitialized.
#region HIGHLIGHT
public Contact(string name) :
base(name)
#endregion
{
}
#pragma warning restore CS8618
Expand Down Expand Up @@ -46,4 +48,23 @@ [NotNull] [DisallowNull]

// ...
}

public class Appointment : PdaItem
{
public Appointment(string name, string location,
DateTime startDateTime, DateTime endDateTime) :
base(name)
{
Location = location;
StartDateTime = startDateTime;
EndDateTime = endDateTime;
}

public DateTime StartDateTime { get; set; }
public DateTime EndDateTime { get; set; }
public string Location { get; set; }

// ...
}
#endregion
}
Loading