You are correct in that is
uses as
and returns a boolean true
if the result is not null
(and vice versa)
However I would suggest you take the resharper rule as a hint or guideline rather than a rule to be followed (I would actually turn it off), and I say that because there is nothing wrong with a NullReferenceException being thrown
consider your code
ISomeInterface interface = this.GetISomeInterfaceInstance();
(interface as ClassImplmentsISomeInterface).Method();
//error thrown above as null reference
now you go ahead and refactor it to
ISomeInterface interface = this.GetISomeInterfaceInstance();
if (interface is ClassImplmentsISomeInterface)
{
(interface as ClassImplmentsISomeInterface).Method();
}
else
{
//else?? else what? how do you recover?
}
There is nothing wrong with an exception if an exception is just that, an exception
Now you might say "handling the else scenario is less costly than an exception" but this is only the case if you can recover, for example, if you could write something like this
ISomeInterface interface = this.GetISomeInterfaceInstance();
try
{
(interface as ClassImplmentsISomeInterface).Method();
}
catch (NullReferenceException)
{
interface = this.GetIOtherInterface().Method();
}
then it would be worth refactoring into a null check as you are actually doing something useful. If your method can't recover gracefully then somewhere your method needs to throw an exception to it's caller to say "hey, something went wrong"