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

olu adesina
olu adesina
23,007 Points

what is the purpose of auto properties

Steven Parker i understand how within curly braces of the getters and setters of properties we can add additional code like conditional statements to control how the variables are used and displayed.

but with auto properties no additional logic is required in the property accessors so how is this different from a field which is public. I can access it to get information and set it i can make it read only

why now use a public field instead

1 Answer

Steven Parker
Steven Parker
220,451 Points

Two advantages to a property come to mind:

  • properties can be virtual and overridden
  • you can give a property's setter different access (protected or private)

Also, more debugging features are available to properties, such as setting a breakpoint on the setter.

:no_bell: And try posting questions without tagging anyone intially, there are several folks around who like to help. You tag someone later if you don't get a good answer in a day or so.