Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Objects Inheritance Throwing Exceptions

what does the "this" do ?

what does the "this" do exactly ? and why do we have to use it in this case ?

3 Answers

Steven Parker
Steven Parker
216,136 Points

Inside a constructor, "this" refers to the object that is being created.

Since the "base" has already loaded in the values for "X" and "Y", the object can be passed to the "Map" function "onMap" to determine if the coordinates are valid.

Marc Martinez
Marc Martinez
12,909 Points

Would that be the same as "Self" in Python?

Steven Parker
Steven Parker
216,136 Points

Not quite. In Python, using the name "self" is just a convention but in C# "this" has special meaning to the compiler.

But how they are used is similar. Assuming the Python code is conventional, they both refer to the current object instance.

ok As You said: Inside a constructor, "this" refers to the object that is being created, so in that case what is the object that is created? ...MapLocation?

Steven Parker
Steven Parker
216,136 Points

Based on the code shown about 2 minutes into this video, you're right, the object that would be created would be an instance of MapLocation.

forget Python for that and answer the question because I have the same question, if I don't want to write the keyword "this", what object I would write

Steven Parker
Steven Parker
216,136 Points

As I said, "this" has special meaning in C#, so you can't substitute anything else.