Why use one over the other?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
== is the identity test. It will return true if the two objects being tested are in fact the same object.
Equals()performs an equality test, and will return true if the two objects consider themselves equal.Identity testing is faster, so you can use it when there’s no need for more expensive equality tests. For example, comparing against
nullor the empty string.It’s possible to overload either of these to provide different behavior — like identity testing for
Equals()–, but for the sake of anybody reading your code, please don’t.Pointed out below: some types like
StringorDateTimeprovide overloads for the==operator that give it equality semantics. So the exact behavior will depend on the types of the objects you are comparing.See also: