Why this matters
Prevents InvalidCastException and makes intent explicit.
Use the as operator or pattern matching for safe casts and guard null results before usage.
Prevents InvalidCastException and makes intent explicit.
Side-by-side examples engineers can pattern-match during review.
var d = (Derived)b; d.Run();if (b is Derived d) { d.Run(); }var x = (Foo)obj;if(obj is Foo foo){ /* use foo */ }From the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.