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# C# Objects Methods Overloading Methods

Arnaldo Zayas
Arnaldo Zayas
577 Points

question regarding the first overload example shown

public int DistanceTo(Point point) { return DistanceTo(point.X, point.Y); } In this example of overloading why do we have to specify return (point.X, point.Y); I tried running it written as return DistanceTo(point); and it doesn't work. I thought it would be implicit that a point object has an X and Y as set up in the fields. I understand that the new overload method allows for this type of call Console.WriteLine(point.DistanceTo(pointTwo)); just would not have thought to write the method this way if It had been presented in an objective. Thank you for your time.

1 Answer

Steven Parker
Steven Parker
231,248 Points

The overloaded version that takes a single "Point" argument is calling the other version (the one that uses two coordinates) to compute the distance.

If you change the return to call "return DistanceTo(point)" instead, that makes it call itself which would cause an infinite recursion loop.