重要な C# トピック

Pattern Matching

Patterns combine type checks, extraction, and conditions without unsafe casts.

How it works

Inspect types and values with declarative patterns.

The switch expression handles null, positive integers, non-empty strings, and a final fallback in one exhaustive expression.

Runnable C# example

C#
Run code →
Program.cs
using System;

class Program
{
    static string Describe(object? value) => value switch
    {
        null => "missing",
        int number when number > 0 => $"positive {number}",
        string { Length: > 0 } text => $"text: {text}",
        _ => "other"
    };

    static void Main() => Console.WriteLine(Describe(12));
}
Expected output
positive 12

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.