Welcome to the Treehouse Community
Looking to learn something new?
i dont understand the getter here why does it have to return and where is it returning ?
the accessor methods video in c# objects i do not understand he point of a getter here ? why and where does it return
William Ruiz10,027 Points
In this video we built how we manage state for an Invader. We are returning _location because, in the future, we will want to know where the invader is located on the grid, relative to where our cannons and our fortress door is located (i.e. have they succeeded, when we shoot at them is it a "hit" or a "miss"). I'm not sure if your broader question is, why do we need to get/set using this pattern at all? Because encapsulation. Jeremy mentions in the video that we could perform additional if/else checks inside the setter, for example, and we might also do additional work inside the getter, if we wanted to for a more complex application. So this pattern protects our private _location from becoming public (and thus prone to being abused by other parts of the code), and it gives us the future flexibility of doing additional work inside the get/set methods if we ever want to.