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

Jakob Kristiansen
Jakob Kristiansen
3,057 Points

obj is Point?

I am wondering how the parameter obj of type System.Object knows that it is a Point object (or any other type). Essentially, how is this evaluated

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

obj does not know about other attributes of the point class e.g. X and Y so it is not entirely clear to me how the above comparison works.

Simon Coates
Simon Coates
28,694 Points

can't tell you. However, values inherit from object and object has a GetType method that returns Type objects. Uncertain if this is how the 'is' keyword operates or whether 'is' is just something built in that you need to accept.

1 Answer

evanpavan
evanpavan
5,025 Points

Jakob, it is actually really simple. The key thing here is that it's doing this check:

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

I bolded Point because in your code Point was lower case referring to the object. In Jason's code Point is uppercase referring to the Point class. The Point class is a 'type'. This 'is' operator is a method that checks to see what class the object passed in belongs to.

So the check that Jason was doing in the lesson was, "Is the object 'obj' of type 'Point' ". This check doesn't need to know if the object has an X or Y value or what those values are. All it needs to know is if the object in question that was passed in is of the Point class.