2

I have this code:

public string foo { get; set; }

Now, I interpret this as my object has a public property called foo, and both it's accessor's are public. If I write this:

private string foo { get; set; }

I interpret that as my object has a private property called foo, and both it's accessor's are private. I understand making the property itself private. What I don't understand is why the accessor's must be more restrictive? If I write:

private string foo { public get; public set; }

I interpret that my object has a private property called foo, and both's it's accessor's are public, which is the behavior that I want. I'd like the private property with public accessors. I mean, if I have to write a Get/Set method, I will. But I'm just confused as to why this is.

PiousVenom
  • 6,888
  • 11
  • 47
  • 86
  • What about that last property is private? Either the whole thing is private, and the getter/setter are actually private, even though you're slapping the `public` accessor on them, or it's public, and the `private` identifier isn't correct... – Servy Apr 24 '13 at 20:32
  • 1
    I think it's just a useful convention. For the sake of code consistency, there should be *some* rule to follow about the order of access levels (the same way there are rules to follow about how `static` should come before `partial` in class definitions), and it makes sense that the rule should be "The property access level must be the least restrictive," because the eye tends to go to that keyword first, and that's the safest option. – Jeremy Todd Apr 24 '13 at 20:36
  • 2
    I do apologize to SO. This question came about due to my ignorance of something very, very basic: [Difference between a field and a property](http://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property-in-c). Now that I've seen this, it is MUCH clearer now. – PiousVenom Apr 24 '13 at 20:42
  • I see no reason for an apology. :) – Matthew Watson Apr 24 '13 at 20:43

2 Answers2

3

A property is actually (under water) nothing more than two methods:

public string foo { get; set; }

will translate into:

public string get_foo() { ... }
public void set_foo(string value) { ... }

These methods can only have ONE access modifier, not a combination of two.

If I remember correcly, C#v1 did not support access modifiers for the getters and setters. There was one access modifers for the property which was used for both functions.

In v2 it was possible to "override" one of getter/setter-pair, this way overrwriting the "other" function. There was no use to override both getters/setters, because in that would render the property-access modifier useless.

Why the access modifier for the getter/setter is more restrictive has, in my opinion, something to do with easier implementing interfaces which always have (implicitly public) properties.

For more info, read: http://msdn.microsoft.com/en-us/library/75e8y5dd(v=vs.80).aspx

Martin Mulder
  • 12,642
  • 3
  • 25
  • 54
0

Why you need such a property

private string foo { public get; public set; }

If you want to have you get set public, then make the property public.

The compiler will first check the access of the property and then its method. If the property is public then its method can have either public or private or any accessor

Kishore Kumar
  • 12,675
  • 27
  • 97
  • 154