Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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
like image 276
ckv Avatar asked Jun 24 '13 06:06

ckv


People also ask

Can we have different access modifiers on Get Set methods of a property?

IS it possible to have different access modifiers on the get/set methods of a property? No. The access modifier on a property applies to both its get and set accessors.

What is the default access modifier for property in C#?

internal is the default if no access modifier is specified. Struct members, including nested classes and structs, can be declared public , internal , or private . Class members, including nested classes and structs, can be public , protected internal , protected , internal , private protected , or private .

Are there access modifiers in C?

Master C and Embedded C Programming- Learn as you go Access Modifiers are used to implement data hiding in object oriented programming. There are three types of access modifiers used in C++. These are public, private and protected.

What are access specifiers What is the default access modifier in a class?

Default access modifier of class is Internal and private for class member. Default access modifier of class is Internal. And private for class member. The internal access specifier hides its member variables and methods from other classes and objects, that is resides in other namespace.


2 Answers

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?

I.e. 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)

like image 94
Damien_The_Unbeliever Avatar answered Oct 22 '22 05:10

Damien_The_Unbeliever


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; 
    }
like image 6
Tigran Avatar answered Oct 22 '22 04:10

Tigran