This is a migrated thread and some comments may be shown as answers.

ReferenceEquals and the Equality Operator

2 Answers 22 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Joni
Top achievements
Rank 1
Joni asked on 10 Aug 2011, 06:21 AM
Hi,

Love the product! I have a question (which would also have been relevant for Reflector).

I was curious to see what went down when the ReferenceEquals method of the Object class was called. See the screenshot below.

Is there a way to drill into the == operator?
Is there a place where that is reflected?
Does this mean that using == is more efficient that using ReferenceEquals?

Cheers

2 Answers, 1 is accepted

Sort by
0
Accepted
Nikolay G Rusev
Telerik team
answered on 11 Aug 2011, 09:31 PM
Hello Joni,

Thank you for the kind words. Now let me answer your feedback.

By inspecting the IL code of the ReferenceEquals method you can see that the == operator is compiled to a simple IL instruction that compares two values. Since the values of the arguments are references then all it does is checking if the two references are equal. The == operator is compiled to the same instruction for every reference type that does not override it.

You can always cast the two operands to object and check reference equality with == instead of ReferenceEquals. I don't think that the difference in performance will be significant so choosing which one to use is a matter of personal preference.

I hope this answers your questions. And if you have more, we'd love to hear again from you.

Greetings,
Nikolay G Rusev
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Joni
Top achievements
Rank 1
answered on 12 Aug 2011, 01:06 AM
Thanks Nikolay!

I have seen people using ReferenceEquals, or even just Object.Equals(null, someObject), instead of something like if(someObject == null).

I figured there must be a performance improvement or something, as the syntax is more verbose than a simple == operator.

Once again, it seems that the good old proven way of doing things is the best.

Thanks!
Tags
General Discussions
Asked by
Joni
Top achievements
Rank 1
Answers by
Nikolay G Rusev
Telerik team
Joni
Top achievements
Rank 1
Share this question
or