Pattern Matching

noun

say it: “PAT-ern MACH-ing” /ˈpætərn ˈmætʃɪŋ/

Checking a value's type, shape or contents in C#, such as with is or in a switch.

What is Pattern Matching in C#?

Pattern Matching — Testing a value's type, shape, or contents while optionally extracting parts of it.

class LabelReader
{
    int LengthOf(object value)
    {
        if (value is string text)
        {
            return text.Length;
        }

        return 0;
    }
}

In a sentence

  • “With pattern matching, score is >= 90 and <= 100 reads almost like English.”

Level: Journeyman · More C# terms at this level

Updated