I’ve made the following declaration for interfaces:
public interface IBasic
{
int Data { get; }
}
public interface IChangeable : IBasic
{
int Data { set; }
}
The compiler says that IChangeable.Data hides IBasic.Data. It’s reasonable. The alternative I’ve found is:
public interface IBasic
{
int Data { get; }
}
public interface IChangeable : IBasic
{
void ChangeData(int value);
}
There is any way to define setter and getters for the same property on different hierarchy on interfaces? Or there are any alternatives to this approach?
You can re-declare it (or rather, tell the compiler that you intend to hide it):
But this is confusing, and you’ll need to use explicit implementations etc, and probably a bit of casting at the caller if you want to use the hidden version. If you went this route, I would recommend exposing both the
getandsetonIChangeable:Re the comments; to expose on the implementing type:
This would also work if you make it (which I prefer):