Part 58 C# Tutorial Why should you override Equals Method


compare two object values c#
compare 2 object c#
check if two objects are the same c#
check if 2 objects are equal c#
object equality c#
c# object.equals vs ==
c# compare two objects for equality
what is the difference between == and equals method in c#
equals vs == c#
.net override equals gethashcode
reference equality in c#
value equality c#
override equals and gethashcode c#

In this video we will learn the difference between
1. Value equality and Reference equality
2. Reasons to override .Equals() method

C#, SQL Server, WCF, MVC and ASP .NET video tutorials for beginners

My Blog:


Xem Thêm Bài Viết Khác:


  1. Very Good Tutorial, thanks a lot! I also liked how you did not just cut out the part where you had to Debug, because you forgot to switch the C1 to C2 when creating the second Customer instance. Searching and solving problems is daily business of Software Engineers.

  2. Hi!!!.
    Structure is not reference type and all class inherit from object class…int is structure then how int is having equals() and tostring() method?

  3. Since you are so bloody good at this stuff, have you thought about creating videos on the Unity Game Engine? There are so many dire videos on this subject on YT, that I think you would be truly above the crowd of any of them if you did Unity videos. Anyway, thank you, and as always, these videos are outstanding.

  4. Equals method will return true,even before overriding.beacause it compares value. In the example it returned false beacuse u haven't assigned any value to c2 intially.

  5. For All Kudvenkat C# Source Code, kindly check my Github Page : Note: I am still adding the source code as I watch the videos.

  6. I really like that you do not hide the wrong code, but you show me, how to keep calm and debug it, when something went wrong. That teaches me, how to handle error messages, how to find out, what was the problem. I think nobody can write perfect code, not even after long years of coding, and this helps me a lot not panicking.

  7. Hi friends i too confuse a little bit when he uses c1 instead c2. i will clarify it as per my knowledge.

    public static void Main(string[] args)
    customer c1= new customer();

    customer c2= new customer();

    False(correct ans)
    False(wrong ans)

    since == checks for the reference equality, so here the condition is c1==c2, since c1 and c2 are two different object reference variables pointing to two different object so the result is false. which is correct result.
    where as the Equals methods compares the values equality, which means it compares the values in the different object: here our two objects have the same values so the result has to be true, but we get false as output, that is because of the default implementation of the Equals method it doesn't know what property is to check for the equality, so we need to override the equals method.

    overriding Equals() method:-
    public override bool Equals(object obj)
    if (obj==null)
    return false;

    if (!(obj is customer))
    {return false;}

    return this.FirstName == ((customer)obj).FirstName && this.LastName ==

    false(correct ans)
    true(correct ans)

    here we check the first and last name of the passed object(c2) with c1. which the values are same on both of the object so it returns true. which is correct answer.

  8. Customer C1 = new Customer();
    C1.FirstName = "Ashish";
    C1.LastName = "Sah";

    Customer C2 = new Customer();
    C2.FirstName = "Ashish";
    C2.LastName = "Sah";

    Both object point to different value. But since Equal operator gives value equality, shouldn't it return True even before it is overloaded ?

  9. When we compare integers, should == return false? Variables being value type would point to different references?
    It doesn't but why? Does it work differently for integer and string?

  10. Thanks for nice tutorial. I have a question that is, why we used '==' instead of equals() in Equals override method? Couldn't we use
    this.FirstName.equals(obj.FirstName) ?

  11. C1.Equals(C2);
    C1 automatically pass hojaye ga is function k andar jsiko hum this.FirstName kar k use kar rahay hain Comparison k liye.

  12. could you be more precise as to what you're refering to as Object? It feels like you're calling everything an Object thus making the teachings very confusing at times.

  13. I have a question. at 8:39, you have created a new instance of customer class, but the values have been set to C1, later the comparisons by "==" and .Equals are done- but they will obviously return false since c2 was never given any values.

    Letr on at 16:02 you realised and corrected the error :).

  14. Small mistake make big different :))
    I think you should change this video title to "Debug in real life" and make another to explain why should override Equals and GetHashCode method too.

    Thank for your video, thank for you explanation.
    And wish you will public Hadoop course.

    Thank you venkat again and again.

  15. Hi, and thank you very much for this tutorial series. I have a question: In the "override int GetHashCode()" method I noticed that you use the XOR "^" operator. Would this not cause issues? And would the single AND "&" operator not be the better choice?

  16. Hello venkat! Your videos are so helpful to me. I have just a quick question here. It would be great if you can clear my doubt!
    What is the advantage of method overrding here since you could create entirely new method called "CompareObjects" in Customer class and call that one. The only advantage of overriding is you get flexibility to use the same name?
    Thanks in Advance!

  17. You've done such an amazing job with all these videos and have explained almost everything there is to c#. Only thing you didn't cover was getHashCode() and hash tables. You've already done so much but if you could make a video on that, that would be perfect. Thank you again!

  18. Thumb up is a must for this episode! I like your debugging, it is funny when it turns out the copied code was not changed correspondingly, but this is how programmers work everyday in reality.

  19. Thank you very much for taking time to give feedback. In the description of this video, I have included the link for ASP .NET, C#, and SQL Server playlists. All the videos are arranged in logical sequence in these playlists, which could be useful to you. Please share the link with your friends who you think would also benefit from them. If you like these videos, please click on the THUMBS UP button below the video. For email alerts, when new videos are uploaded, you may subscribe to my channel.


Please enter your comment!
Please enter your name here