C
C#14mo ago

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

q
7 Replies
Thinker
Thinker14mo ago
Iirc it's not possible for a property getter to have an accessibility equal to or less than the setter (besides public)
MODiX
MODiX14mo 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æ
OP14mo ago
it can be protected if the setter isnt
Thinker
Thinker14mo ago
In which case you'll have a property which anyone can set but only derived classes can read
fæ
OP14mo ago
if the property itself is protected, does it extend to both get and set?
Thinker
Thinker14mo ago
Yeah, then both the getter and setter with be protected
Accord
Accord14mo 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.
Want results from more Discord servers?
Add your server