Nullable and Pattern Matching
Property Patterns
A property pattern checks selected properties on an object.
property pattern
A property pattern matches an object by reading named properties.
Property Patterns
PropertyPatterns.cs
Replay: real traced execution (multi-file project)
using System;
class Order
{
public string Status { get; }
public int Total { get; }
public Order(string status, int total)
{
Status = status;
Total = total;
}
public override string ToString()
{
return Status + ":" + Total;
}
}
class Program
{
static void Main()
{
int total = 120;
Order order = new Order("paid", total);
string result = order is { Status: "paid", Total: >= 100 } ? "priority" : "standard";
Console.WriteLine($"total={total}");
Console.WriteLine($"result={result}");
}
}
using System;
class Order
{
public string Status { get; }
public int Total { get; }
public Order(string status, int total)
{
Status = status;
Total = total;
}
public override string ToString()
{
return Status + ":" + Total;
}
}
class Program
{
static void Main()
{
int total = 40;
Order order = new Order("paid", total);
string result = order is { Status: "paid", Total: >= 100 } ? "priority" : "standard";
Console.WriteLine($"total={total}");
Console.WriteLine($"result={result}");
}
}
using System;
class Order
{
public string Status { get; }
public int Total { get; }
public Order(string status, int total)
{
Status = status;
Total = total;
}
public override string ToString()
{
return Status + ":" + Total;
}
}
class Program
{
static void Main()
{
int total = 150;
Order order = new Order("paid", total);
string result = order is { Status: "paid", Total: >= 100 } ? "priority" : "standard";
Console.WriteLine($"total={total}");
Console.WriteLine($"result={result}");
}
}
total ← 120
21{22 static void Main()23 {24 int total→ 120 = 120; //@total=40, 15025 Order order = new Order("paid", total);26 string result = order is { Status: "paid", Total: >= 100 } ? "priority" : "standard";public Order(string status, int total)
8public Order(string statuspaid, int total120)9{10 Status = statuspaid;11 Total = total120;12}order ← paid:120, result ← priority
24 int total = 120; //@total=40, 15025 Order order→ paid:120 = new Order("paid", total);26 string result→ priority = orderpaid:120 is { Status: "paid", Total: >= 100 } ? "priority" : "standard";2728 Console.WriteLine($"total={total120}");29 Console.WriteLine($"result={resultpriority}");30}outputtotal=120 result=priority
total ← 40
21{22 static void Main()23 {24 int total→ 40 = 40;25 Order order = new Order("paid", total);26 string result = order is { Status: "paid", Total: >= 100 } ? "priority" : "standard";public Order(string status, int total)
8public Order(string statuspaid, int total40)9{10 Status = statuspaid;11 Total = total40;12}order ← paid:40, result ← standard
24 int total = 40;25 Order order→ paid:40 = new Order("paid", total);26 string result→ standard = orderpaid:40 is { Status: "paid", Total: >= 100 } ? "priority" : "standard";2728 Console.WriteLine($"total={total40}");29 Console.WriteLine($"result={resultstandard}");30}outputtotal=40 result=standard
total ← 150
21{22 static void Main()23 {24 int total→ 150 = 150;25 Order order = new Order("paid", total);26 string result = order is { Status: "paid", Total: >= 100 } ? "priority" : "standard";public Order(string status, int total)
8public Order(string statuspaid, int total150)9{10 Status = statuspaid;11 Total = total150;12}order ← paid:150, result ← priority
24 int total = 150;25 Order order→ paid:150 = new Order("paid", total);26 string result→ priority = orderpaid:150 is { Status: "paid", Total: >= 100 } ? "priority" : "standard";2728 Console.WriteLine($"total={total150}");29 Console.WriteLine($"result={resultpriority}");30}outputtotal=150 result=priority