One of the advantages that i hear about properties is that they can be used for databinding, while i hear no explanation of why beyond "that's how it works". Is it that way because there is no choice (i.e. a limitation of the .NET VM) or was it really designed that way, and if so why?
+1
A:
Using properties instead of public fields is good practice - I would imagine that when data binding was being designed, they chose to try to encourage that good practice. Allowing fields to be bound directly would be tantamount to saying that the separation between interface and implementation didn't matter, and that it was fine to just expose everything publicly.
That's just a guess, mind you.
Although there's PropertyDescriptor
there could equally have been FieldDescriptor
, and you can fetch fields by reflection just as easily as properties... so I doubt that it was a technical problem.
Jon Skeet
2010-01-23 07:29:54
I understand why to use properties, it's just that that's often pushed as a reason to do so and i wanted to know why.
RCIX
2010-01-23 07:33:00
@RCIX: My point is that by making it *another* advantage, the design team have encouraged their use further.
Jon Skeet
2010-01-23 07:34:58
Properties are more readable and make code a lot more compact than having lots of mutators and accesors around. IMHO.
jasonco
2010-01-23 07:37:31
@jasonco: I don't think anyone was suggesting accessor methods - the choice is usually public properties backed by private fields vs public fields. I believe that's what the question was about.
Jon Skeet
2010-01-23 09:08:07
@Jon, I probably didn't understand the question correctly.
jasonco
2010-01-23 09:31:26