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# Intermediate C# System.Object Object.Equals

Jamie Wyton
Jamie Wyton
3,011 Points

this and that...

What was the point of creating another Point that object? couldn't we just of measured the object in question by using obj?

return this.X == obj.X && this.Y == obj.Y;

And what's with the as command, whats that for?

2 Answers

Steven Parker
Steven Parker
231,198 Points

A generic object (like "obj") does not have "X" and "Y" properties.

So to get access to those properties, the Point named "that" is created. And "as" is the type conversion operator. It works much like a cast, so "obj as Point" means "obj treated as if it were a Point", which makes it possible to assign it to "that".

It would not look as nice, but you could also write:

    return this.X == (obj as Point).X && this.Y == (obj as Point).Y;
Jamie Wyton
Jamie Wyton
3,011 Points

so is that 'as' works much in the same way as when the MapLocation constructor calls the OnMap method using the whole object as an argument but then receives it in Point form when it gets there?

Steven Parker
Steven Parker
231,198 Points

That's slightly different, since MapLocation is derived from Point. So essentially every MapLocation object is already also a Point, and no conversion is needed.