I don’t understand why Resharper suggest me to “check for reference equality instead” in this code:
if ( typeToTranslate.Equals( typeof(string) ) )
{
//do something
}
Why this should be better:
typeToTranslate == typeof(string)
————EDIT————
This is the method stub:
protected IType TranslateType(Type typeToTranslate)
{
if (typeToTranslate == null) throw new ArgumentNullException("typeToTranslate");
//do some stuff
if (typeToTranslate.Equals(typeof(string)))
{
//do some stuff
}
//return some stuff
}
Object.Equalsis a more general kind of equality than reference equality: ifx == ythenx.Equals(y), but the converse isn’t necessarily true. However, as documented in MSDN Library:Because ReSharper categorizes the “Check for reference equality instead” inspection option under “Common Practices and Code Improvements”, my guess is that ReSharper is letting you know that it suffices to use reference equality to compare types; you don’t need the more general kind of equality implied by the
Equalsmethod (even though for types the two are equivalent).