In C#, the is
type testing operator can be used to check if the run-time type of an object is compatible with a given type. If the object is not null, then the
is operator performs a cast, and so performing another cast following the check result is redundant.
This can impact:
Use pattern macthing to perform the check and retrieve the cast result.
if (x is Fruit) // Noncompliant
{
var f = (Fruit)x; // or x as Fruit
// ...
}
if (x is Fruit fruit)
{
// ...
}
is, as, typeof and casts