ReSharper 2020.3 Help

Code Inspection: The given expression of 'is' operator is always of the provided type

In a compatibility check expr is type between reference types, expr on the left might be of a type assignable to type on the right.

According to the C# documentation, "The is statement is true if expr is non-null and the object that results from evaluating the expression can be converted to type; otherwise, it returns false."

In other words, the only case when this check might evaluate to false is when expr is null.

To reveal the actual semantics of the check in this case, we should replace the is check with as simple null check.

This warning is similar to the compiler warning CS0183: The given expression is always of the provided ('type') type (which ReSharper also detects), but it deals with reference types, while the compiler warning occurs when there is conversion of value types.

private void Test(String str) { if (str is Object) { // do something } }
private void Test(String str) { if (str != null) { // do something } }
Last modified: 08 March 2021