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#

jordan weitz
jordan weitz
1,431 Points

invader.Location = location;

ok so this is mad confusing for me. The code in the title is using the Location method from the invader class. He doesn't specify whether or not we are using the "Get" or "Set" though, just that we are using Location. I think this can be done because in the code above we are effectively setting the value so perhaps c# can assume we are using the "Set" method. However, directly proceeding this we flip this code around "location = invader.Location;" and this is supposed to be referencing the "Get" method I assume. How does the console or whatever know that this is for the "Get" method? for example if I declare that x = 5, isn't that the same as if i declare 5 = x?

1 Answer

Steven Parker
Steven Parker
229,644 Points

A single equal sign is an assignment operator, and assignments always work right-to-left, evaluating the term on the right, and storing it where the term on the left specifies it should go.

So any property on the right side will be accessed with the "get" method, and one on the left side will be accessed using the "set" method.

And assuming "x" is a variable of an appropriate type, "x = 5" is a valid assignment, but "5 = x" is not, since you can't change the value of a literal number 5.