Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Jannung Kusdiantoro
Jannung Kusdiantoro
6,743 Points

i don't understand how overriding Equal method works, can someone explain to me? about "that", "obj", and how we use

i don't understand how overriding Equal method works, can someone explain to me? about "that", "obj", and how we use that. please explain the logic

Steven Parker
Steven Parker
231,210 Points

Are you talking about something you saw in a course?

A link to the course page would be helpful, and if it's a video, an approximate time index would also be good.

2 Answers

Hi

Assuming you have 2 strings:

string String1 = "I am string"; string String2 ="I am a string";

A string is an object, and you now have 2 object. An object is a Reference to memory location where the actual value of the string is stored. Example, String1 may have a value of XX123ABC and String2 may have a value of YY345EFG. XX123ABC and YY345EFG are the memory addresses where the actual string "I am string" is stored.

Now if you ask C# Object.equal methad whether or not String1 == String2 , tou are going to get "false" because XX123ABC != YY345EFG

This is a case where you want to override the equal (==) behavior

In this case, the .NET framework has done this for you already ... this is what String1 .equal(String2) does ... it is an override of the default == behaviour.

Not let change this a bit.

string String1 = "I am string "; string String2 ="I am a string";

Note the extra spaces in String1. Now let say your app want this 2 string to be considered equals if someone try to compare them

In this case, if you try the built in String1 .equal(String2) , you are going to get false.

Hence you need to override the built in String1 .equal(String2) method and provide logic to remove or ignore the extra spaces when you make the comparison

I hope this helps. If this naswers your question, please mark the question as answered.

Thank you