Code inspection: Simplify 'IsInstanceOfType()' invocation
This inspection reports invocations like IsAssignableFrom(value.GetType())
. Such invocations can be simplified either as IsInstanceOfType(value)
or as value is string
. All those expressions are identical in terms of semantics and performance, but the suggested alternatives improve readability.
There is an additional safety benefit in the suggested transformations, because both IsInstanceOfType()
and is
will return false
if value
is null
, thus avoiding a potential NullReferenceException
, which otherwise will be thrown by the GetType()
invocation.
public bool IsCompatibleWithString(object value)
{
return typeof(string).IsAssignableFrom(value.GetType());
}
public bool IsCompatibleWithString(object value)
{
return value is string;
}
Last modified: 08 August 2024