I my code I’m using the System.Function method Debug.Assert(..) for verify the input parameter at the beginning of a method (see following example code snipped):
public class TestClass : IInterface
{
}
public class Verifier
{
public static void Verify(IInterface objectToVerify)
{
Debug.Assert((objectToVerify is TestClass), "Passed object must be type of TestClass");
// ReSharper (Version 7.1.1) marks here "Expression is always false
if (!(objectToVerify is TestClass))
{
return;
}
// do something ...
}
}
If I comment out the Debug.Assert statement the ReSharper warning disappears.
In my opinion, ReSharper has to ignore this Debug.Assert statement, because also if the Debug.Assert statement is not fulfilled, the code beneath is executed (e.g. in Release-mode)
What is your opinion? Or is there a alternative implementation idea?
ReSharper is smart enough to know that
Debug.Assert()will stop execution ifobjectToVerifyis not aTestClass. Therefore, the expression in yourifstatement is indeed alwaysfalse(otherwise theifstatement wouldn’t be reached in the first place).You can work around the warning by writing something like: