简体   繁体   中英

Why cant different access modifiers be specified for automatically implemented property in C#

Why is the below expression invalid? I know how to resolve the errors but wondering the rationale behind now allowing this statement.

public int Number {public get;protected set; }

I dont have a use case or application to elaborate on why this should be allowed. But the compiler throws 2 errors:

Error   2   The accessibility modifier of the 'LambdaExpressions.Program.Person.Number.get' accessor must be more restrictive than the property or indexer 'LambdaExpressions.Program.Person.Number'    LambdaExpressions\LambdaExpressions\Program.cs  66  39  LambdaExpressions

and

Error   1   Cannot specify accessibility modifiers for both accessors of the property or indexer 'LambdaExpressions.Program.Person.Number'  LambdaExpressions\LambdaExpressions\Program.cs  66  24  LambdaExpressions

Because you already got to specify one of the modifiers first:

public int Number {public get;protected set; }
//^
//here

What would that modifier be modifying if you have modifiers on both accessors?

Ie imagine an even odder example:

public int Number {protected get;protected set; }

Exactly what part or concept of Number is now public ?

Per @dash's comments, from MSDN :

By default these accessors have the same visibility, or access level: that of the property or indexer to which they belong

You can use accessor modifiers only if the property or indexer has both set and get accessors. In this case, the modifier is permitted on one only of the two accessors .

( My emphasis )

Because providing access modifier to a property not only delegates it automatically to get and set , but also implies a restriction that: even if any modifier applied on them has to be more restrictive that that one defined on the property itself.

Having this in mind, you can do

public int A {
    get; 
    private set; 
}

but you can not do (by design of the language)

    //both modifer can not have be more restrictive then property itself
    //non sence
    public int A {
        protected get; 
        private set; 
    }

yo can not do

    //one of modifiers is less restrictive
    //again non sence
    protected int A {
        public get; 
        set; 
    }

Your example is redundant. The access modifier is already public, specifying it again is pointless.

However, the real problem is that the C# language only allows you to specify more restrictive modifiers, thus the following code is illegal:

private int Number {public get; set;}

This has the side effect of also being illegal if you specify the same level (ie public and public). It must be more restrictive.

You can also only specify one modifier, because it's pointless to place a access modifier on the property itself otherwise.

public int Number {protected get; private set;} // How is it public?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM