0

According to this page in MSDN, turns out that the accessibility of a specific accessor has to be more restrictive than the indexer and that the property itself.

Now, I understand what it means and several other cases (see questions like C# - Improving encapsulation of property in this example?, ERROR: the accessibility modifier of the set accessor must be more restrictive than the property or indexer and Why is internal protected not more restrictive than internal?).

What I'm failing to understand (and it is the question here) is... is there any reason why this is enforced?

It is just not obvious to me what's wrong with having something like this:

// THIS IS WRONG -- I want this to be set by this class and its derivated
protected MyPropertyType MyProperty 
{ 
    get; // this is protected, by the accessibility of the property
    protected set; // this is wrong -- why?
}

I think this is the kind of question where the answer may be so obvious that I'll feel really dumb once I get the answer... but I just couldn't find it around and just cannot get it myself. So... fire at will!

Cœur
  • 37,241
  • 25
  • 195
  • 267
Alpha
  • 7,586
  • 8
  • 59
  • 92
  • I always figured it was an intentional feature imposed on the grammar to avoid redundancy. – FishBasketGordo Aug 29 '11 at 20:52
  • It's redundant, so why would you want this? – CodesInChaos Aug 29 '11 at 20:53
  • @FishBasketGordo Possibly, but that sounds more like a "readability" enhancement rather than a real compiler error level. – Alpha Aug 29 '11 at 23:42
  • @CodeInChaos It is redundant, but I don't see a reason why it wouldn't be allowed. Also, this is out of the scope of the question, but the property level could be used for specifying a default access level to the setter and getter and access modifiers on them could set the real level. No redundancy, vast options. – Alpha Aug 29 '11 at 23:44

1 Answers1

4

What I'm failing to understand (and it is the question here) is... is there any reason why this is enforced?

I suspect this is for clarity. Your syntax:

protected MyPropertyType MyProperty 
{ 
    get;
    protected set;
}

Would, effectively, be the same as not specifying the accessibility of the setter, ie:

protected MyPropertyType MyProperty 
{ 
    get;
    set;
}

However, when looking at the code, it appears that you were intending to restrict the accessibility. Having the compiler enforce that a specific accessor require more restrictive accessibility likely reduces the overall number of bugs by forcefully simplifying the code by reducing the redundancy.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • I guess you're right, it does make sense that if you're "restricting" the access level further, then it should be less or equal (although an equal level is not allowed here). However, we are setting an access level, are we necessarily restricting it access further? That makes sense for classes (as the accessibility has to be the intersection of the class itself and the methods/properties), but for what is a property if not its setters and getters? – Alpha Aug 29 '11 at 23:46
  • @Alpha: Using the current mechanism, you can always have any 2 accessibilties - if you want a setter "more" accessible than the getter, you just set the property to that, and go. The advantage here is that the property's accessibility shows that the property, at some level, is accessible to the "outside world" at the level specified - even if a portion of it is more restricted. – Reed Copsey Aug 29 '11 at 23:55