Question

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?

Was it helpful?

Solution

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.)

OTHER TIPS

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top