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#

Evan Welch
Evan Welch
1,815 Points

If properties can stand in for fields, why do we ever need fields?

If properties are just better form and work the same as fields, why are fields necessary?

1 Answer

Simon Coates
Simon Coates
8,377 Points

Microsoft's discussion of them includes "A field defines a storage location... A property definition contains declarations for a get and set accessor that retrieves and assigns the value of that property". A discussion at stackoverflow seemed to imply that there's always a field lurking somewhere under the hood of a property. I had assumed maybe properties were a new feature, but they seem to have been part of c# 1. If a value can't be modified or is private with few rules about its access, then the field might be safe to access or you might not gain anything by writing a property. if properties are meant to protect a field and apply validation, a complete absence of rules or transformation would mean a property would add extra code without adding value. Apart from this, a couple people discussing this at https://stackoverflow.com/questions/2166433/why-ever-use-fields-instead-of-properties suggest that ref/out and readonly only worked on fields.