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 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
231,198 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.