Reputation: 1183
When I have automatic propertie and I try to access it from within it's class, it seems like an overhead, because I use a function to access a member of my class instead of just accessing it directlly.
If this is correct, maybe I should consider not to use automatic properties in such cases?
Upvotes: 2
Views: 229
Reputation: 2344
If you are following Object Oriented Principles, you would violate the Principle of Encapsulation by allowing access to your internal members directly. The Property mechanism (getters and setters methods) provide the proper access to these members protecting the internal members from direct access.
Upvotes: 0
Reputation: 887195
Automatic properties are no different from ordinary properties in this regard.
Don't worry about it; the JITter will typically inline the property methods anyway.
Upvotes: 3
Reputation: 1499770
Have you measured any theoretical overhead and found it to be significant? That's the key to making performance-based decisions.
In this case, I'd thoroughly expect the JIT to inline automatically-implemented properties, removing any performance overhead. (I seem to remember seeing a case with float
/ double
where this wasn't the case, but that was a while ago - and even then the overhead was pretty small.)
Upvotes: 5
Reputation: 19956
You are right on that. However, some mechanisms need properties, for example XML serializer won't serialize public members...
Other thing is encapsulation - you never know in advance what is the final destination of each property of your class, so if you create it as property at first, you can go into set/get implementation later.
Upvotes: 2