Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# How to restrict interface properties

I have one interface which is inherited by a few classes, some of the properties however are being set only by the constructor of the classes. I will never change the values of those properties outside of the constructor so I want to restrict the properties by having them with a private setter, but the interface doesn't allow me to use properties with any type of modifiers how can i work around this ?

like image 467
PreqlSusSpermaOhranitel Avatar asked Mar 17 '26 16:03

PreqlSusSpermaOhranitel


1 Answers

Interfaces are there to describe things that any implementing type must be able to do. It's not capable of defining thinks that it can't do.

The interface can define that there must be a given property getter of an appropriate name and type, but it can't specify that there must not be a public setter for that property.

You're certainly welcome to provide documentation with this interface suggesting that implementors have a private setter, or even that they never allow the value to change outside of a constructor, but there's no way for an interface to specify such a constraint.

like image 73
Servy Avatar answered Mar 19 '26 04:03

Servy