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
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.
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 .
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.
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.
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)
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With