If I have a property that I want to let inheritors write to, but keep readonly externally, what is the preferred way to implement this? I usually go with something like this:
private object m_myProp;
public object MyProp
{
get { return m_myProp; }
}
protected void SetMyProp(object value)
{
m_myProp = value;
}
Is there a better way?
private object m_myProp;
public object MyProp
{
get { return m_myProp; }
protected set { m_myProp = value; }
}
Or in C# 3.0
public object MyProp {get; protected set;}
This is definitely the way to go.
public object MyProp {get; protected set;}
If you're on an older version of C# then this is the way to go.
private object _myProp;
public object MyProp
{
get { return _myProp; }
protected set { _myProp = value; }
}
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