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 Encapsulation with Properties Auto-Properties

Get Set unclear

In the video I was confused when Jeremy changed the get set function to an auto-function. I was wonder how it still worked even though you're not returning _location or assigning _location to value

4 Answers

Steven Parker
Steven Parker
220,425 Points

Auto-properties are not associated with a named field.

The place where the value is stored has no name that can be accessed by the code, and only the generated get and set methods know how to get to it. This is actually a good thing, because it guarantees all access will be done through the property.

Anuj Bhusari
Anuj Bhusari
12,240 Points

I am still unclear about it. Steven Parker The thing that I do not understand is we are making use of the auto-properties. So declaring a variable then setting a getter and setter for it just by one line, how does that even work? I mean we haven't declared a variable. Moreover how will the application know what variable to get and what variable to set since there is no variable used?

Anuj Bhusari
Anuj Bhusari
12,240 Points

For example if I have a customer class that has two member variables say firstName and lastName. Both of these variables are of type String. So when I can set the property name according to my needs, I do it as follows: public string FName{get; set;} and public string LName {get; set;}

In the above example how will the compiler know which variable I am trying to set, since both the variables are of the same type and contain similar data.

Steven Parker
Steven Parker
220,425 Points

The property does have a variable, but only the compiler knows the name. The variable is created automatically when you declare an auto property.

The compiler uses the variable in the "get" and "set" methods it creates for you, so the name of the property is enough for it to know which one to read or set.

think of it this way, the get set property is actually a variable in itself, notice how you are not putting the parenthesis at the end of it like you wold in a normal method, its like a variable named "Location" but with the ability of extending to have the get set with respective code blocks, it is declared to be public and instructed to hold only objects of the Maplocation type, when you use it in main() and decide to set its value to an instance of the Maplocation class, say new Maplocation (2,1, map), and then you analyse the resulting statement that will be compiled, you will notice it will become;

public Maplocation Location = new Maplocation(2,1,map);

and when you get its value you will notice it will be exactly like reading the contents of any other variable. this just makes sure you are only using the property to access contents of the Location variable, this is why you are able to delete the field all together.

Ok I understand the set portion how do you Get the value when needed?

Steven Parker
Steven Parker
220,425 Points

You can always get the value of an auto-property by referencing it just as you would a field. You cannot get to the backing variable directly, but you don't need to.