Why are the accessible private field and private setter not available from a static method?

Below is the compiler error:

    private ObservableEventListener Listener { get; private set; }

    public static void Register()
    {
        Log.Listener = new ObservableEventListener();
        Log.Listener.EnableEvents(Log, EventLevel.LogAlways, EventKeywords.None); 
    }

      

Error 26 Accessibility modifier "MyEventSource.Listener.set" must be stricter than property or index "MyEventSource.Listener"

But this compiles just fine:

    private ObservableEventListener Listener;

    public static void Register()
    {
        Log.Listener = new ObservableEventListener();
        Log.Listener.EnableEvents(Log, EventLevel.LogAlways, EventKeywords.None);

    }

      

Seems like there is more safety in setting methods? There are always redundant lines of code / warnings .. why is this an error?

+3


source to share


4 answers


You have indicated private

in the customizer that is okay except that you made an element private

.

private

is not more restrictive than private

that violates the rule stated in the error, namely:



Accessor modifier 'MyEventSource.Listener.set' accessor must be more restrictive than property or index 'MyEventSource.Listener'

Perhaps the language designers should have had equal constraints, but they didn't, so the compilation failed.

+5


source


Ultimately, the only sensible answer here is "because the language designers have indicated that the accessibility statement for property accessories should be more restrictive than the property itself." It is for this reason that this is a mistake. Why did they choose this ... well, you can try reading the annotated spec, but ... meh.



I think it just doesn't make sense to have a modifier public

in a property private

, so a clearly less restrictive (more accessible) one is meaningless; and an equally restrictive overkill: you add keywords, presumably with the intent to do something, and it won't have a result - possibly a mistake.

+6


source


As usual with compilation questions, the answer is: spec says so!

Section 10.7.2 (focus)

  • The access modifier must declare an accessibility that is strictly more restrictive than the declared accessibility of the property or indexer itself . More precisely:
    • If a property or index has a declared public availability, the modifier can be either protected internal, internal, protected, or private.
    • If a property or index has a protected internal accessibility declared, the modifier-modifier can be either internal, protected, or private.
    • If a property or indexer has internal or protected accessibility declared, the modifier must be private.
    • If the property or pointer is declared private, the modifier cannot be used.
+6


source


Placing an access modifier on a getter or setter is intended to allow you to restrict it further than prose as a whole. So it doesn't make sense to take a private property and then try to make its setter private, because it's already closed.

Correct usage, public getter, private setter:

public ObservableEventListener Listener { get; private set; }
^^ What you want BOTH Get/Setter to be         ^^ but you can make one more restrictive

      

Wrong:

private ObservableEventListener Listener { get; private set; }
^^ You want BOTH Get/Setter private             ^^ So what does this mean if you already made them private?

      

+2


source







All Articles