Nullable and Pattern Matching
Property Patterns
A property pattern checks selected properties on an object.
Property Patterns
PropertyPatterns.cs
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 = ;
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 = ;
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 = ;
Order order = new Order("paid", total);
string result = order is { Status: "paid", Total: >= 100 } ? "priority" : "standard";
Console.WriteLine($"total={total}");
Console.WriteLine($"result={result}");
}
}
property pattern
A property pattern matches an object by reading named properties.