C
C#2y ago

❔ Difference between public property with protected setters and getters and protected property?

q
7 Replies
Thinker
Thinker2y ago
Iirc it's not possible for a property getter to have an accessibility equal to or less than the setter (besides public)
MODiX
MODiX2y ago
Thinker
REPL Result: Failure
class C
{
public int X { protected get; protected set; }
}
class C
{
public int X { protected get; protected set; }
}
Exception: CompilationErrorException
- Cannot specify accessibility modifiers for both accessors of the property or indexer 'C.X'
- Cannot specify accessibility modifiers for both accessors of the property or indexer 'C.X'
Compile: 441.349ms | Execution: 0.000ms | React with ❌ to remove this embed.
fæ
OP2y ago
it can be protected if the setter isnt
Thinker
Thinker2y ago
In which case you'll have a property which anyone can set but only derived classes can read
fæ
OP2y ago
if the property itself is protected, does it extend to both get and set?
Thinker
Thinker2y ago
Yeah, then both the getter and setter with be protected
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?