This property in a type with no access modifier (thus internal
access):
class SomeType {
private int length;
internal int Length {
get { return length; }
set length = value; }
}
}
allows all types within the assembly of SomeType to use get
and set
accessors. Problem: how to restrict the access to set
to only types derived from SomeType (and SomeType indeed)?
internal int Length {
get { return length; }
protected set length = value; }
}
is rejected by the compiler, because protected
is said to be less restrictive than internal
(supposedly: protected
has an intersection with internal
, but is not entirely included in internal
--> Derived types can exist beyond the scope of internal
).
What would be the code to have get accessed by any type within the assembly, and set only by derivated types within the assembly?
Edit: after reviewing the answers, I think I need to add another characteristic of the property, since it may make a difference in the solution: the type of the property is actually SomeType
. The edited code is:
class SomeType {
private SomeType length;
internal SomeType Length {
get { return length; }
set length = value; }
}
}
If the property is declared public
, then the compiler issues an error (the property type SomeType is less accessible the property Length).