C
C#16mo ago
zzzz

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

q
7 Replies
Thinker
Thinker16mo ago
Iirc it's not possible for a property getter to have an accessibility equal to or less than the setter (besides public)
MODiX
MODiX16mo 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.
zzzz
zzzzOP16mo ago
it can be protected if the setter isnt
Thinker
Thinker16mo ago
In which case you'll have a property which anyone can set but only derived classes can read
zzzz
zzzzOP16mo ago
if the property itself is protected, does it extend to both get and set?
Thinker
Thinker16mo ago
Yeah, then both the getter and setter with be protected
Accord
Accord16mo 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?