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#

Benjamin Orimoloye
Benjamin Orimoloye
23,328 Points

Inconsistency with other Languages

While i understand the advantages of using C# short code forms like the Auto-property instead of the "getPropertyName()" and "setPropertyName()", the later seems easier to comprehend especially coming from PHP and Java that I'm not sure uses the auto-property

1 Answer

Allan Clark
Allan Clark
10,810 Points

One of the main advantages of Auto properties is that the language treats it more like a variable than a method. When it comes to simple objects it will seem like there isn't much difference (because there isn't), but when you start getting a larger scope of the language and the framework around it you will be able to see its advantages.

For example Entity Framework uses Lazy Loading, which means that when it pulls an object with a child object from the database, it will not grab the child object until it is referenced. This is requires using those properties.

A more low level example would be if you needed to send a field of an object as a ref/out parameter to a method call. You wouldn't be able to send in a getter method call because there would be no variable for the method to interact with.

here is an article and stack overflow post that goes into more detail.

https://blog.codinghorror.com/properties-vs-public-variables/

https://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property-in-c

Hope this helps! Happy Coding