Today I learned that you can use pattern matching in C# to check for a type and cast to it in the same expression. See the docs for more details.

Microsoft even has a lint rule for it.

if (x is Fruit)  // Noncompliant
{
  var f = (Fruit)x; // or x as Fruit
  // ...
}
if (x is Fruit fruit)
{
  // ...
}