Add or improve property matching code listings
MODIFY
src/Chapter07.Tests/Listing07.29.PropertyPatternMatching.Tests.cs
src/Chapter07.Tests/Listing07.29.PropertyPatternMatching.Tests.cs
@@ -23,8 +23,9 @@ public class ProgramTests
)
{
Assert.AreEqual(expectedResult, ExpenseItem.ValidateExpenseItem(
- new ExpenseItem(itemId, new Employee(employeeId, employeeName, role),
- itemName, (decimal)amount, DateTime.Now.AddDays(-daysPassed))
+ new ExpenseItem(itemId, itemName,
+ (decimal)amount, DateTime.Now.AddDays(-daysPassed),
+ new Employee(employeeId, employeeName, role))
));
}

MODIFY
src/Chapter07/Listing07.28.PositionalPatternMatching.cs
src/Chapter07/Listing07.28.PositionalPatternMatching.cs
@@ -6,7 +6,8 @@ using System.Drawing;
public static class PointHelper
{
- public static void Deconstruct(this Point point, out int x, out int y) =>
+ public static void Deconstruct(
+ this Point point, out int x, out int y) =>
(x, y) = (point.X, point.Y);
public static bool IsVisibleOnVGAScreen(Point point) =>

MODIFY
src/Chapter07/Listing07.29.PropertyPatternMatching.cs
src/Chapter07/Listing07.29.PropertyPatternMatching.cs
-using AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_12;
-
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter07.Listing07_29;
+#region INCLUDE
public record class Employee
{
public int Id { get; set; }
@@ -17,29 +16,28 @@ public record class Employee
public class ExpenseItem
{
public int Id { get; set; }
-
public string ItemName { get; set; }
-
- public Employee Employee { get; set; }
-
public decimal CostAmount { get; set; }
-
public DateTime ExpenseDate { get; set; } = DateTime.Now;
+ public Employee Employee { get; set; }
- public ExpenseItem(int id, Employee employee, string name, decimal amount, DateTime date) =>
- (Id, Employee, ItemName, CostAmount, ExpenseDate) = (id, employee, name, amount, date);
+ public ExpenseItem(
+ int id, string name, decimal amount, DateTime date,
+ Employee employee) =>
+ (Id, Employee, ItemName, CostAmount, ExpenseDate) =
+ (id, employee, name, amount, date);
- #region INCLUDE
-#pragma warning disable IDE0170 // Property pattern can be simplified
public static bool ValidateExpenseItem(ExpenseItem expenseItem) =>
expenseItem switch
{
// Note: A property pattern checks that the input value is not null
// Expanded Property Pattern
{ ItemName.Length: > 0, Employee.Role: "Admin" } => true,
+ #pragma warning disable IDE0170 // Property pattern can be simplified
// Property Pattern
- { ItemName.Length: > 0, Employee: {Role: "Manager" },
+ { ItemName: { Length: > 0 }, Employee: {Role: "Manager" },
ExpenseDate: DateTime date }
+ #pragma warning restore IDE0170 // Property pattern can be simplified
when date >= DateTime.Now.AddDays(-30) => true,
{ ItemName.Length: > 0, Employee.Name.Length: > 0,
CostAmount: <= 1000, ExpenseDate: DateTime date }
@@ -47,7 +45,6 @@ public class ExpenseItem
{ } => false, // This not null check can be eliminated.
_ => false
};
-#pragma warning restore IDE0170 // Property pattern can be simplified
- #endregion INCLUDE
}
+#endregion INCLUDE

MODIFY
src/Chapter07/Table07.03.PatternMatchingWithIsOperator.cs
src/Chapter07/Table07.03.PatternMatchingWithIsOperator.cs
@@ -38,7 +38,10 @@ public class PaternMatchingExamples
{
public Employee(string firstName, string lastName, string id) : base(firstName, lastName)
{
+ Id = id;
}
+
+ public string Id { get; }
}
// 1. Declaration Pattern

Add or improve property matching code listings