C# : Property vs. field
Recently there was an email-thread in the internal C# group on the usage of property vs. field.
The question raised was in case a class implements properties, is it still ok to go ahead and access the field directly from other methods of the same class. The thread also covered when it is ok to add properties.
This is a decision we take everyday when writing code. Some great people pitched in to give there opinion. The outcome is something like this
- In case you are designing a class library and the consumer is not under your control then add properties for all public fields to prepare for future needs to add checks to field access
- In case you can version (change) the client code as well, do not add properties just because its good, or because you think you might need functionality of checks, lazy loading in the future. In case you do not need these features right-away allow clients to directly use fields. With the new refactoring features of VS2005 changing all references to the field to properties is a non-issue.
- With the above two filtering you are sure that once you have a property its definitely going to do some additional work and so only use the property from everywhere (including methods from same class).