50

How can I create a public getter and a private setter for a property? Is the following correct?

public String Password
{
    set { this._password = value; }
}

private String Password
{
    get { return this._password; }
}
Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Muneer
  • 7,384
  • 7
  • 38
  • 62
  • 2
    your text asks for public String Password {get; private set} but your code is for public String Password { set; private get;} one of those is obviously wrong. – Yaur May 21 '11 at 15:37
  • @Yaur: Good catch, I didn't notice that. But either one will work using the same syntax. – Cody Gray - on strike May 21 '11 at 16:36

6 Answers6

118

Yes it is possible, even with auto properties. I often use:

public int MyProperty { get; private set; }
Anders Abel
  • 67,989
  • 17
  • 150
  • 217
  • can you explain what this guy is saying about your answer? http://stackoverflow.com/questions/1073392/how-often-do-you-see-abuse-of-c-sharp-shorthand-getters-setters is he saying that this is not alright to do? im confused – Brandon Ling Mar 31 '13 at 21:46
  • 3
    That question and Jon's answer says that there are cases where this is not okay, specifically if the value should be readonly (not possible to set after ctor has run) it is wrong to use an auto property. As with any language feature there are both correct and incorrect uses. – Anders Abel Apr 01 '13 at 20:15
14

Yes, as of C# 2.0, you can specify different access levels for the getter and the setter of a property.

But you have the syntax wrong: you should declare them as part of the same property. Just mark the one you want to restrict with private. For example:

public String Password
{
    private get { return this._password; }
    set { this._password = value; }
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
4
public String Password
{
    private set { this._password = value; }
    get { return this._password; }
}

or you can use an auto-implemented property:

public String Password { get; private set; }
Jason Moore
  • 3,294
  • 15
  • 18
2
public String Password
{
    private set { this._password = value; }
    get { return this._password; }
}

MSDN:

The get and set methods are generally no different from other methods. They can perform any program logic, throw exceptions, be overridden, and be declared with any modifiers allowed by the programming language.

Edit: MSDN quote is just to clarify why geter and setter can have different access mdofiers, Good point raised by @Cody Gray:

Yes, properties can perform program logic and throw exceptions. But they shouldn't. Properties are intended to be very lightweight methods, comparable to accessing a field. The programmer should expect to be able to use them as they would a field without any noticeable performance implications. So too much heavy program logic is strongly discouraged. And while setters can throw exceptions if necessary, getters should almost never throw exceptions

YetAnotherUser
  • 9,156
  • 3
  • 39
  • 53
  • Not really if you think about it - they are no different from other functions. – YetAnotherUser May 21 '11 at 15:13
  • 1
    Yes, properties *can* perform program logic and throw exceptions. But they *shouldn't*. Properties are intended to be very lightweight methods, comparable to accessing a field. The programmer should expect to be able to use them as they would a field without any noticeable performance implications. So too much heavy program logic is strongly discouraged. And while setters can throw exceptions if necessary, getters should almost *never* throw exceptions. – Cody Gray - on strike May 21 '11 at 15:14
  • +1 @Cody Gray - in general I agree, was just quoting what msdn says about it. – YetAnotherUser May 21 '11 at 15:16
  • Fair enough. The only point of that statement is to describe their implementation. They are implemented as two separate methods under the hood. It's not giving design recommendations, that's found elsewhere on Microsoft's site. But most people never bother to look at those, evidenced by lots of the code I see posted here. Just because you can do something doesn't mean you should. – Cody Gray - on strike May 21 '11 at 15:17
2
public string Password { get; private set; }
Pang
  • 9,564
  • 146
  • 81
  • 122
Tahbaza
  • 9,486
  • 2
  • 26
  • 39
1

To gain an 'Excavator' badge, and to make the answer up to date - readonly fields encapsulated by a get-only property

private readonly int myVal;
public int MyVal get { return myVal; }

may be now (as of C# 6.0) shortened to

public int MyVal { get; }
Marc Wittke
  • 2,991
  • 2
  • 30
  • 45