This is typically done by chaining constructors. You have every constructor call "up" through a common constructor. For example:
public class MyThing : MyBaseThing
{
public MyThing()
: this(null, null)
{
}
public MyThing(string pk, string rk)
: base(pk, rk)
{
Status = "3";
}
public string Status { get; set; }
}
That works well if you can chain constructors. But sometimes you'll have constructors that can't be chained. If that's the case, then you make sure that each of your "leaf" constructors calls a private Init()
method of some kind, that does common initialization. That's kind of a pain in the neck at times, when you have a readonly
field, because readonly
fields can only be modified in the constructor--not in a method that's called by a constructor.
A deleted answer proposed using a property with an explicit backing field, rather than using an auto-implemented property. That's a perfectly valid and quite reasonable thing to do, negative comments notwithstanding. Sure, it's not as pretty as an auto-implemented property, but it's very effective. It's what we did before we had auto-implemented properties, and there's no reason not to do it if you really need a default value for a property. In case you can't see the deleted answer:
public class MyThing : MyBaseThing
{
private string _status = "3";
public string Status
{
get { return _status; }
set { _status = value; }
}
// constructors go here.
}