I'd like to have an
interface IFoo
{
string Foo { get; }
}
with an implementation like:
abstract class Bar : IFoo
{
string IFoo.Foo { get; private set; }
}
I'd like the property to be gettable through the interface, but only writable inside the concrete implementation. What's the cleanest way to do this? Do I need to "manually" implement the getter and setter?
interface IFoo
{
string Foo { get; }
}
abstract class Bar : IFoo
{
public string Foo { get; protected set; }
}
almost as you had it but protected and drop the IFoo. from the property in the class.
I suggest protected assuming you only want it accessable from INSIDE the derived class. If, instead, you'd want it fully public (able to be set outside the class too) just use:
public string Foo { get; set; }
Why the explicit implementation of the interface? This compiles and works without problems:
interface IFoo { string Foo { get; } }
abstract class Bar : IFoo { public string Foo { get; protected set; } }
Otherwise, you could have a protected/private property for the class, and implement the interface explicitly, but delegate the getter to the class's getter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With