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 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
231,269 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
231,269 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
231,269 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
231,269 Points

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