Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Varying a Property's get/set Accessibility

It is a common occurrence wherein you may want to publicly expose a property's getter but internally restrict the setter. (The last refactoring challenge had a good use for this.)  I regularly see people provide a publicly exposed property - with only a getter - and then add an internally available method for setting that property. Alternatively, as long as you're only "tightening" access, you can simply pre-append the appropriate access keyword in front of the get/set, accordingly. For example:

In the above code, the getter is publicly available while the setter is only internally available to the assembly in which it's defined. You can also use protected, private and logical combinations, if needed.

Billy


Posted 02-15-2007 4:55 PM by Billy McCafferty
Filed under:

[Advertisement]

Comments

Michal Grzegorzewski wrote re: Varying a Property's get/set Accessibility
on 02-16-2007 11:03 AM

I would say it's just a naming convention which shows me as a client of your code your intent. It's a kind of agreement between you - provider of this property, and me - the user. When I see 'private', 'internal', etc. I read it that way: 'If you use this method/property/whatever you may get some unpredictible results, so be careful'. It's not a restriction - using DynamicMethod along with ILGenerator.Emit (or by CreateDelegate) we've got dynamic access with almost the same performance as when using direct calls - so, why not use dynamic access? Using tools such as TypeWrapper (http://devlicio.us/blogs/michal_grzegorzewski/archive/2006/12/12/typewrappertool.aspx) it's even simpler.

awesome linkbuilding site wrote re: Varying a Property's get/set Accessibility
on 10-01-2013 7:40 AM

ptMaub Very neat blog post.Really thank you!

crorkz matz wrote re: Varying a Property's get/set Accessibility
on 08-06-2014 6:45 AM

90SwQ6 Awesome article.Really thank you! Great.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)