C
C#10mo ago

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

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