Part 58 C# Tutorial Why should you override Equals Method

preview_player
Показать описание
Tags
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# 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

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

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

Рекомендации по теме
Комментарии
Автор

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.

Csharp-video-tutorialsBlogspot
Автор

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.

Trzbne
Автор

Very Nice Explanation of C# in simple English

praveenkumars
Автор

That little debugging session was actually helpful, nice to know how you deal with such problems ;)

styr
Автор

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?

jaystrikes
Автор

This is an excellent training session, your explanation is crystal clear sir.

ericbauwens
Автор

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.

jasonleelawlight
Автор

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!

bklynmarine
Автор

best teacher and brilliant teaching of thanks

iamsmzubair
Автор

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();
c1.FirstName="raj";
c1.LastName="sekhar";

customer c2= new customer();
c2.FirstName="raj";
c2.LastName="sekhar";
}



output:-
False(correct ans)
False(wrong ans)

explanation:
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 ==
((customer)obj).LastName;
}

output:
false(correct ans)
true(correct ans)

explanation:
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.

Rajsekharpakalapati
Автор

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.

Linkario
Автор

Very necessary to be said; made the mistake of forgetting this the other day and took forever to find out what went wrong... no good comparing two objects expecting a different comparison than the compiler is going to do...

Truthiness
Автор

please put the text version for rest of the slides as well as there are for the previous ones in the description box. They are really helpful.. thank you

busytuber
Автор

Hi Venkat !!
first of all Thanks for the great efforts !!
your videos are very helpful !! It is certainly an easy and effective way to boost myself-confidence !!
all the best !!

sushilkulkarni
Автор

you are !!. Can you please record video for GETHASHCODE also?
It would be great help. Thanks you so much for creating such a nice series.
Waiting for WCF videos.

arunntbe
Автор

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 :).

upendrawatwe
Автор

At minute 9:05. Why do you code C1.firstname again as "simon" ? Should the second time be C2.FirstName = Simon"; in line 13?

flyboy
Автор

Great video! Thanks a lot.
Just a quick comment: the "is" operator will check for null object so we can eliminate the first condition. Also, I believe we should use Typeof operator instead of "is" operator, i.e. objects of same class should be considered unequal, and the test should proceed only if the passed object is of the same type.

mouradbarakat
Автор

very good just the hashcode at the end i do not know what it is for lol

samduss
Автор

Two Customer objects with the same values for FirstName and LastName will return the same hash code using the overridden GetHashCode method. Is this the intended outcome?

eddyproft