Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

property: private method or private get/set?

If I want to set my property in a Class private, so it should be only possible to use and set this property in this class, what is the better way? This

public string Name { private get; private set }

or

private string Name { get; set }

hmmm and there is also

private string Name { private get; private set }
like image 243
silla Avatar asked Sep 03 '12 18:09

silla


3 Answers

It's worth noting that originally, C# wouldn't let you set different accesses on a getter or setter, so the only possible choices were:

public string Name { get; set; }
protected internal string Name { get; set; }
internal string Name { get; set; }
protected string Name { get; set; }
private string Name { get; set; }

(For that matter, you couldn't have automatic properties and always had to do the writing to and from a backing field yourself, but we'll ignore that just because we'll have shorter examples that way).

It is often useful to have different accesses for the two, most often a more restrictive setter than getter, and so the likes of

public string Name { get; private set; }

was introduced.

Now, by extension of that, it would seem logical enough to allow:

public string Name { private get; private set; }
private string Name { private get; private set; }

However, what are these two expressing?

The second isn't too bad, it's just needlessly repetitious. Still though, it's quite likely that some confused thinking got us there (most likely an incomplete refactoring). Good code is as much about expressing what you are doing as making a computer do something (if anything, more so), better to have it express clearly.

Hence if you end up with the likes of { private get; private set; } then it'd likely be worth looking at again and thinking about what you really want to say here. Hurrah for it being a compiler error.

The first case is even worse. It says "this property is public, except for the setter that is private, and the getter that is private". That's not an exception, "it's this thing, except for all the time" is no real expression of anything. Doubly hurrah the compiler for not letting us do it.

like image 111
Jon Hanna Avatar answered Oct 19 '22 00:10

Jon Hanna


Have you tried compiling your examples? Only the middle one will translate.

If you want to specify extra accessibility level keyword, you can only do it on one of the accessors (getter/setter), and that level of the accessor must be more restrictive than the accessibility of the entire property.

Here you see the rules: Restricting Accessor Accessibility

like image 28
Jeppe Stig Nielsen Avatar answered Oct 19 '22 00:10

Jeppe Stig Nielsen


public string Name { get; private set; }

This is what I think you are wanting to do.

There is no point trying to make the get private when the property is public unless you only want your class to see it. In that situation you should use:

private string Name { get; set; }

Update: On second read, you definitely want the second example.

like image 36
Mr. Mr. Avatar answered Oct 18 '22 23:10

Mr. Mr.