property: private method or private get/set?

37,900

Solution 1

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

Solution 2

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.

Solution 3

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.

Share:
37,900
silla
Author by

silla

hi

Updated on February 17, 2021

Comments

  • silla
    silla about 3 years

    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 }
    
  • silla
    silla over 11 years
    yeah you are right. Somehow I cannot compile if if use private get; private set only if I use private for get OR for set
  • Jeppe Stig Nielsen
    Jeppe Stig Nielsen over 11 years
    Just to be pedantic: Without specifying different access on a getter or setter, there's a fifth possibility: protected internal string Name { get; set; }
  • Jon Hanna
    Jon Hanna over 11 years
    @JeppeStigNielsen Yep. I'd half a notion that protected internal wasn't in C#1.0 and came in later, but a quick search seems to suggest it was always there.
  • Jeppe Stig Nielsen
    Jeppe Stig Nielsen over 11 years
    The internal one is now accidentally private.
  • Hand-E-Food
    Hand-E-Food over 11 years
    FYI, I'm not the downvoter. I think you mean private void set_Name(string value) { }.
  • Jon Hanna
    Jon Hanna over 11 years
    @JeppeStigNielsen lol. Sometimes one should just leave well alone.
  • John Demetriou
    John Demetriou over 8 years
    @silla That is because you are setting the Property with one Access Modifier while setting all possible accessors with a different one. Setting it as public with get and set private negates the public part, so it won't compile. If you set it as private, get and set are automatically private, so no need to set them....
  • Alex78191
    Alex78191 over 6 years
    public string Name { private get; private set; } won't compile Cannot specify accessibility modifiers for both accessors of the property or indexer 'Program.Name'
  • Peter
    Peter over 6 years
    nice reading, though might be handy for newbies to tell about the most obvious usage : public string name { get; private set} simply meaning classes can read this property but the class itself sets it. which could be by a constructor or the result of methods inside this class.