Resharper suggestion: check for reference equality instead
Asked Answered
M

2

24

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
 }
Much answered 30/11, 2012 at 15:7 Comment(7)
My Resharper doesn't suggest this?Losse
@Thomas I use version 7.0.1Much
Is typeToTranslate actually a Type object, and not some other type?Acculturate
@Acculturate It is a Type object: i added a more comprehensive code snippetMuch
Possible duplicate: #9234509 (accepted answer links an article which explains this pretty well)Windtight
Why don't you ask this question at the JetBrains Resharper forum? devnet.jetbrains.net/community/resharperAcis
why downvote? please explain so I can improve my question...Much
A
28

Object.Equals is a more general kind of equality than reference equality: if x == y then x.Equals(y), but the converse isn't necessarily true. However, as documented in MSDN Library:

A Type object that represents a type is unique; that is, two Type object references refer to the same object if and only if they represent the same type. This allows for comparison of Type objects using reference equality.

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 Equals method (even though for types the two are equivalent).

Aviate answered 30/11, 2012 at 15:28 Comment(0)
S
16

From the System.Type documentation:

A Type object that represents a type is unique; that is, two Type object references refer to the same object if and only if they represent the same type. This allows for comparison of Type objects using reference equality.

This means that "string".GetType() and typeof(string) return the same reference. There is only a single instance of the System.Type object for System.String within an AppDomain.

As to why ReSharper says it's "better" to use == instead of .Equals()? My guess is because == is more efficient and does not risk throwing a NullReferenceException if typeToTranslate were null (in your case).

Spadiceous answered 30/11, 2012 at 15:18 Comment(4)
I check that typeToTranslate is not null so it could be only for efficiencyMuch
@Much : that's enough for R# to suggest!Dirtcheap
The suggestion also appears if you write Equals(typeToTranslate, typeof(string)), so it's unlikely that NullReferenceException is the reason.Aviate
@Michael -- Fair enough. Unfortunately, R# doesn't have a "why is resharper suggesting this?" entry for this particular suggestion. It would be good to hear their reasoning. To me, it makes some intuitive sense to "prefer" == and reference equality, if possible, as reference equality is a simpler operation.Spadiceous

© 2022 - 2024 — McMap. All rights reserved.