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
4 changes: 2 additions & 2 deletions src/Chapter07/Listing07.01.DerivingOneClassFromAnother.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public class PdaItem
// Define the Contact class as inheriting the PdaItem class
#region HIGHLIGHT
public class Contact : PdaItem
#endregion
#endregion HIGHLIGHT
{
public string? Address { get; set; }
public string? Phone { get; set; }
}
#endregion
#endregion INCLUDE
}
4 changes: 2 additions & 2 deletions src/Chapter07/Listing07.02.UsingInheritedMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public static void Main()
Contact contact = new Contact();
#region HIGHLIGHT
contact.Name = "Inigo Montoya";
#endregion
#endregion HIGHLIGHT

// ...
}
}
#endregion
#endregion INCLUDE
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ public class Customer : Contact
{
// ...
}
#endregion
#endregion INCLUDE
}
6 changes: 3 additions & 3 deletions src/Chapter07/Listing07.04.ImplicitBaseTypeCasting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public static void Main()
Contact contact = new Contact();
#region HIGHLIGHT
PdaItem item = contact;
#endregion
#endregion HIGHLIGHT
// ...

// Base types must be cast explicitly to derived types
#region HIGHLIGHT
contact = (Contact)item;
#endregion
#endregion HIGHLIGHT
// ...
}
}
#endregion
#endregion INCLUDE
}
4 changes: 2 additions & 2 deletions src/Chapter07/Listing07.05.DefiningCastOperators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public static implicit operator UTMCoordinates(
{
#region EXCLUDE
return null!; //return the new UTMCoordinates object
#endregion
#endregion EXCLUDE
}
}
#endregion
#endregion INCLUDE

class UTMCoordinates
{
Expand Down
8 changes: 4 additions & 4 deletions src/Chapter07/Listing07.06.PrivateMembersAreNotInherited.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class PdaItem
#region EXCLUDE
// Justification: Disabled pending constructor
#pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
#endregion
#endregion EXCLUDE
private string _Name;

public string Name
Expand All @@ -21,7 +21,7 @@ public string Name
}
#region EXCLUDE
#pragma warning restore CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
#endregion
#endregion EXCLUDE
}

public class Contact : PdaItem
Expand All @@ -38,8 +38,8 @@ public static void Main()
// ERROR: 'PdaItem._Name' is inaccessible
// due to its protection level
// contact._Name = "Inigo Montoya";
#endregion
#endregion HIGHLIGHT
}
}
#endregion
#endregion INCLUDE
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class PdaItem
public PdaItem(Guid objectKey) => ObjectKey = objectKey;
#region HIGHLIGHT
protected Guid ObjectKey { get; }
#endregion
#endregion HIGHLIGHT
}

public class Contact : PdaItem
Expand All @@ -27,20 +27,20 @@ public void Save()
#region HIGHLIGHT
using FileStream stream = File.OpenWrite(
ObjectKey + ".dat");
#endregion
#endregion HIGHLIGHT
// ...
stream.Dispose();
}
static public Contact Copy(Contact contact)
#region HIGHLIGHT
=> new Contact(contact.ObjectKey);
#endregion
#endregion HIGHLIGHT

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

public class Program
Expand All @@ -52,8 +52,8 @@ public static void Main()
#region HIGHLIGHT
// ERROR: 'PdaItem.ObjectKey' is inaccessible
// Console.WriteLine(contact.ObjectKey);
#endregion
#endregion HIGHLIGHT
}
}
#endregion
#endregion INCLUDE
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ public sealed class DerivedCommandLineParser
{
// ...
}
#endregion
#endregion INCLUDE
}
6 changes: 3 additions & 3 deletions src/Chapter07/Listing07.09.OverridingAProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ public class PdaItem
{
#region HIGHLIGHT
public virtual string Name { get; set; }
#endregion
#endregion HIGHLIGHT
// ...
}

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

// ...
}
#endregion
#endregion INCLUDE
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ public static void Main()
$"{ contact.FirstName } { contact.LastName}");
}
}
#endregion
#endregion INCLUDE
}
2 changes: 1 addition & 1 deletion src/Chapter07/Listing07.11.OverrideVersusNewModifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ SuperSubDerivedClass superSubDerivedClass
BaseClass.DisplayName();
}
}
#endregion
#endregion INCLUDE
}
2 changes: 1 addition & 1 deletion src/Chapter07/Listing07.12.SealingMembers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ class C : B
//{
//}
}
#endregion
#endregion INCLUDE
}
4 changes: 2 additions & 2 deletions src/Chapter07/Listing07.13.AccessingABaseMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public override string ToString()
{
#region HIGHLIGHT
return base.ToString() +
#endregion
#endregion HIGHLIGHT
NewLine + Country;
}
}
#endregion
#endregion INCLUDE
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Contact : PdaItem
#region HIGHLIGHT
public Contact(string name) :
base(name)
#endregion
#endregion HIGHLIGHT
{
}
#pragma warning restore CS8618
Expand Down Expand Up @@ -66,5 +66,5 @@ public Appointment(string name, string location,

// ...
}
#endregion
#endregion INCLUDE
}
4 changes: 2 additions & 2 deletions src/Chapter07/Listing07.15.DefiningAnAbstractClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_15
// Define an abstract class
#region HIGHLIGHT
public abstract class PdaItem
#endregion
#endregion HIGHLIGHT
{
public PdaItem(string name)
{
Expand All @@ -25,6 +25,6 @@ public static void Main()
// item = new PdaItem("Inigo Montoya");
}
}
#endregion
#endregion INCLUDE
#pragma warning restore CS0168
}
12 changes: 6 additions & 6 deletions src/Chapter07/Listing07.16.DefiningAbstractMembers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public PdaItem(string name)
public virtual string Name { get; set; }
#region HIGHLIGHT
public abstract string GetSummary();
#endregion
#endregion HIGHLIGHT
}

public class Contact : PdaItem
Expand All @@ -26,7 +26,7 @@ public Contact(string name)
: base(name)
{
}
#endregion
#endregion EXCLUDE
public override string Name
{
get
Expand Down Expand Up @@ -56,7 +56,7 @@ public string FirstName
}
}
private string? _FirstName;
#endregion
#endregion HIGHLIGHT

#region HIGHLIGHT
public string LastName
Expand All @@ -71,7 +71,7 @@ public string LastName
}
}
private string? _LastName;
#endregion
#endregion HIGHLIGHT
public string? Address { get; set; }

#region HIGHLIGHT
Expand All @@ -81,7 +81,7 @@ public override string GetSummary()
+ $"LastName: { LastName + NewLine }"
+ $"Address: { Address + NewLine }";
}
#endregion
#endregion HIGHLIGHT

// ...
}
Expand Down Expand Up @@ -110,5 +110,5 @@ public override string GetSummary()
+ $"Location: { Location }";
}
}
#endregion
#endregion INCLUDE
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ public static void List(PdaItem[] items)
}
}
}
#endregion
#endregion INCLUDE
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public class PdaItem : object
{
// ...
}
#endregion
#endregion INCLUDE
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public static void Save(object data)
}
#region EXCLUDE
Console.WriteLine(data);
#endregion
#endregion EXCLUDE
}
#endregion
#endregion INCLUDE

public static object Encrypt(string data)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Chapter07/Listing07.20.TuplePatternMatching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ static public void Main(params string[] args)
// ...
#region HIGHLIGHT
if ((args.Length, args[Action]) is (1, "show"))
#endregion
#endregion HIGHLIGHT
{
Console.WriteLine(File.ReadAllText(DataFile));
}
#region HIGHLIGHT
else if ((args.Length, args[Action].ToLower(), args[FileName])
is (2, "encrypt", string fileName))
#endregion
#endregion HIGHLIGHT
{
string data = File.ReadAllText(DataFile);
File.WriteAllText(fileName, Encrypt(data).ToString());
Expand All @@ -36,7 +36,7 @@ public static string Encrypt(string data)
// See Chapter 19 for actual encryption implementation
return $"ENCRYPTED <{data}> ENCRYPTED";
}
#endregion
#endregion EXCLUDE
}
#endregion
#endregion INCLUDE
}
8 changes: 4 additions & 4 deletions src/Chapter07/Listing07.21.PositionalPatternMatching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public Person(string firstName, string lastName)
}
public string FirstName { get; }
public string LastName { get; }
#endregion
#endregion EXCLUDE

#region HIGHLIGHT
public void Deconstruct(out string firstName, out string lastName) =>
(firstName, lastName) = (FirstName, LastName);
#endregion
#endregion HIGHLIGHT
}

public class Program
Expand All @@ -30,11 +30,11 @@ public static void Main()
// Positional Pattern Matching
#region HIGHLIGHT
if (person is (string firstName, string lastName))
#endregion
#endregion HIGHLIGHT
{
Console.WriteLine($"{firstName} {lastName}");
}
}
}
#endregion
#endregion INCLUDE
}
4 changes: 2 additions & 2 deletions src/Chapter07/Listing07.22.PropertyPatternMatching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public static void Main()
// Positional pattern matching
#region HIGHLIGHT
if (person is {FirstName: string firstName, LastName: string lastName })
#endregion
#endregion HIGHLIGHT
{
Console.WriteLine($"{firstName} {lastName}");
}
// ...
#endregion
#endregion INCLUDE
}
}
}
2 changes: 1 addition & 1 deletion src/Chapter07/Listing07.23.RecursivePatternMatching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static void Main()
// ...
}
// ...
#endregion
#endregion INCLUDE
}
}
}
Loading