Prev | Current Page 368 | Next

Jon Skeet

"C# in Depth: What you need to master C# 2 and 3"

For this reason,
my personal convention is not to use any access modifiers unless I
need to; that way, the code highlights (by way of an access modifier) when
I??™ve explicitly chosen to make something more public than it might be.
Specifying the access of a property getter or setter is the one exception to
this rule??”if you don??™t specify anything, the default is to give the getter/
setter the same access as the overall property itself.
Note that you can??™t declare the property itself to be private and make the getter public
??”you can only make a particular getter/setter more private than the property. Also,
you can??™t specify an access modifier for both the getter and the setter??”that would just
be silly, as you could declare the property itself to be whichever is the more public of
the two modifiers.
This aid to encapsulation is long overdue. There??™s still nothing in C# 2 to stop
other code in the same class from bypassing the property and going directly to whatever
fields are backing it, unfortunately. As we??™ll see in the next chapter, C# 3 fixes this
in one particular case, but not in general. Maybe in C# 4??¦
We move from a feature you may well want to use fairly regularly to one that you
want to avoid most of the time??”it allows your code to be absolutely explicit in terms
of which types it??™s referring to, but at a significant cost to readability.


Pages:
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380