Consider that I have an interface that contains the following property
interface IFoo
{
Int32 Id { get; }
}
Now say I want to create an IMutableFoo interface. Logically I would think that the following was correct:
interface IMutableFoo: IFoo
{
Int32 Id { set; }
}
My thought is that it would inherit Id and then in my child interface I’d make it settable. To my surprise, this did not work. Instead I get a warning letting me know that I’m in fact overriding the Id in IFoo with the Id in IMutableFoo. I tried changing { set; } to { get; set; } with the same results. How do I do this right? In Java, I would simply add a setId method. How do I do this in C#? Thanks!
Re-declaration of Id in the descendent interface indeed hides the one in the parent.
I use two different workarounds for this, entailing tradeoffs that I do not particularly like.
1 – Using an abstract or even a non-abstract class instead of a mutable interface.
The biggest drawback is that the users of your library can no longer program to an interface.
2 – Using a method instead of a property.
This is not ideal, because the setter is not idiomatic, and looks disconnected from the getter.