Code inspection: Type pattern and casts can be merged
This inspection reports switch cases that already check a value's type, but then repeat casts of the same switched expression inside the case body or guard. Using a pattern variable in the case label makes the code shorter and clearer, because the typed value is introduced once and reused everywhere in that case.
Example
switch (obj)
{
case int x when ((int)obj) > 0:
Console.WriteLine((int)obj);
break;
}
switch (obj)
{
case int x when x > 0:
Console.WriteLine(x);
break;
}
Quick-fix
Use the pattern variable instead of repeating the cast.
01 April 2026