c# interview question :- Difference between == VS .Equals()

preview_player
Показать описание

See our other Step by Step video series below :-

What is the Difference between == VS .Equals() ?. This is a interesting c# interview questions which is making rounds in the .NET interviews.

"==" compares if the object references are same while ".Equals()" compares if the contents are same.

We are also distributing a 200 page Ebook ".NET Interview Question and Answers". If you want this ebook please share this video in your facebook/twitter/linkedin account and email us on
Рекомендации по теме
Комментарии
Автор

Do not miss our Interview Question video series

dnfvideo
Автор

string datatype does not always compare content. It is comparing reference when you use == operator. The reason it is returning true resides in the concept of string pooling.
If you are creating another string with the same content then there is no second object being created ; it will first check if there is any string with the same content; if yes it will return the address of that object (i.e. both references will be pointing to same object on the heap). String pooling is the reason behind making string immutable.

sandeep
Автор

== is a binary operator that is a static class member whose default implementation for reference types (such as the string type) is to check for reference equality (ie do the two parameters reference the same instance on the heap). The Equals method, on the other hand is an instance member, whose default implementation on reference types is also to check for reference equality. Both can be overridden and for the string class in particular, the Equals method has been overrided to evaluate the string data itself (rather than just th reference) for equality.

crikey
Автор

Thank you for clearing this up! Talking about this in computer science class and was having trouble following the documentation. Very clear explanation.

chrisp
Автор

Great explanation sir, your all technical sessions are nice. Thanks for sharing the information.

TwinkleStar
Автор

operator== can be overloaded, i.e., it's resolved at compile time. Equals is a virtual method, hence resolved at runtime.

For string specifically it may not matter because it's sealed, but if you want to do reference comparison, you should use object.ReferenceEquals so that overloads/overrides don't affect you.

klocugh
Автор

questions like this, sometimes can confuse on the job interview even if you are with 3+ year experience, that means that we have to honing our skills each day

SergeiKjtydghk
Автор

Great Video.

I have been coding for years now, and consider myself good, but have been trying to discipline myself more, understanding the basic structure of C# better and how to properly utilize it.

This has been one that has bugged me, and your video finally cleared it up for me. It all makes sense now.

The way I see it, if I am doing a string comparison, do .Equals. This way, if I were to change the type of the object down the line, I wouldn't have to change any comparisons, and would just have to make sure the .Equals logic for the new type does as I expect (I rarely work in type object)

AngeofDrkness
Автор

That what many developers would expect content comparison rather than reference comparison. String have data and developers would be surprised with a alternate behavior.

dnfvideo
Автор

I would say that == actually is an operator which can be overwritten. String type overwrites == operator therefore it is equivalent to Equal()

vchirilov
Автор

Great explanation, man!
Thanks. God bless you!

viniciusm.m.
Автор

Smita, I believe that is because they are both value types and not reference types, so they have the same pointer.

soozler
Автор

Woww


Your explanation is super sir...

grtentertainments
Автор

Thank you so much - fantastic explanation.

louisecrowe
Автор

Very good !!! This is what I was looking for !!!

rafapiotrowicz
Автор

Could you please explain the output for below object obj1 = new StringBuilder("Hello"); object obj2 = "Hello"; Console.WriteLine(obj1 == obj2); // false false

shubhamsonalkar
Автор

It was a great explanation but I have a question why string always compares content?

manishjoshi
Автор

So, If we do:

object obj1 = "Hello World";
object obj2 = "Hello World";

Console.WriteLine(obj1 == obj2);

The above will print 'true' even though we have two different(obj1 & obj2) instances which according to your explanation should result in false?

MuhammadAli-dbkk
Автор

Thank you. Great videos!

question:
in the following code there is TRUE for both.
How come the object reference is the same?

                object o1 = "text";
                object o2 = "text";

                Console.WriteLine(o1 == o2);        // TRUE
                  // TRUE

EinatRosenberg
Автор

What about long x=2;
int y =2;
What == and .Equals() will return

irajeshtailor