Sign in to follow this  
Daniel Miller

[.net] C#: public get and private set

Recommended Posts

Is it possible to have a property with a public Get method, but a private Set method? I know there are so many ways around this, but I have a field for which I want internal bounds checking and external visibility visibility to other classes, but no one needs else needs to directly alter its value. I figured this way was the cleanest, but as I said there are a billion other ways to do this. [Edited by - Daniel Miller on July 12, 2005 1:23:11 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Not exactly. Make the field "internal"; this way you can access it from anywhere in your assembly. Provide no setter for your property and instead manipulate the field directly.


Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Not exactly. Make the field "internal"; this way you can access it from anywhere in your assembly. Provide no setter for your property and instead manipulate the field directly.


Is a bad idea, especially if the field might change at a later date. An internal method would be fine, if the local assembly is the one who needs to access it.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I don't see why - an internal field is something only *you* should be touching.

Is there a really good argument against direct private field access you've got in mind?

Share this post


Link to post
Share on other sites
Quote:
I have a field for which I want internal bounds checking and external visibility


The OP *wants* bounds-checking done on any modifications made by other objects. Hence, an internal field as opposed to an internal method is not satisfactory.

If the bounds checking is not needed, then an internal field would suffice, though this is still arguably bad practice from an encapsulation/reusability standpoint.

Share this post


Link to post
Share on other sites
I do need bounds-checking, but I should not have said the word "external". I meant that in the English sense, not in the .NET sense (and what a forum to make that slip-up on!). I should have said, "visibility to other classes". My mistake.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this