Is it possible to give private access to a base class setter and only have it available from the inheriting classes, in the same way as the protected keyword works?
public class MyDerivedClass : MyBaseClass
{
public MyDerivedClass()
{
// Want to allow MyProperty to be set from this class but not
// set publically
public MyProperty = "abc";
}
}
public class MyBaseClass
{
public string MyProperty { get; private set; }
}
Why don't you use protected
?
public string MyProperty { get; protected set; }
protected (C# Reference)
A protected member is accessible within its class and by derived class instances.
You only need to make the setter as protected like:
public class MyDerivedClass : MyBaseClass
{
public MyDerivedClass()
{
// Want to allow MyProperty to be set from this class but not
// set publically
MyProperty = "abc";
}
}
public class MyBaseClass
{
public string MyProperty { get; protected set; }
}
See also Access Modifiers (C# Reference)
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