Pattern Matching Classifier
Classify mixed values with property and relational patterns.
Introduction
Modern C# patterns keep type extraction and validation in one expression.
The string property pattern checks Length after confirming the runtime type.
Runnable C# example
Program.cs
using System;
class Program
{
static string Classify(object? value) => value switch
{
int and >= 18 => "adult age", string { Length: 0 } => "empty text",
string text => $"text({text.Length})", null => "null", _ => "other"
};
static void Main() => Console.WriteLine(Classify("CSharp"));
}
text(6)Practice with the debugger
Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Triangle Classification Classify three side lengths as invalid, equilateral, isosceles, or scalene. Easy