Sign in to follow this  

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

This topic is 4571 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
I believe that C# 2.0 supports this, but not 1.0 or 1.1. As you said, there are a billion other ways to do this, such as protected Set___ methods.

Share this post


Link to post
Share on other sites
That's what I expeced to hear, because I tried a bunch of ways and none of them compiled. Thanks for your help anyways (I wish C# 2.0 was installed here at work).

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
Guest Anonymous Poster
Hm. That part had completely slipped my attention!

Sorry.

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
Sign in to follow this