Reputation: 13443
Please see question embedded in comment below.
public class CustomException : Exception
{
private readonly string _customField;
public CustomException(string customField, string message)
: base(message)
{
// What's the best way to reuse the customField initialization code in the
// overloaded constructor? Something like this(customField)
}
public CustomException(string customField)
{
_customField = customField;
}
}
I'm open to considering alternative implementations that reuse the base constructor and minimize initialization code. I'd like to keep the _customField readonly, which is not possible if I extract a separate initialization method.
Upvotes: 5
Views: 902
Reputation:
Factor it out into a separate method, and call that method from both constructors.
public class CustomException : Exception
{
private readonly string _customField;
public CustomException(string customField, string message)
: base(message)
{
Init(out _customField, customField);
}
public CustomException(string customField)
: base()
{
Init(out _customField, customField);
}
private Init(out string _customField, string customField)
{
_customField = customField;
}
}
Upvotes: 3
Reputation: 116118
public CustomException(string customField) : this(customField,"")
{
}
Upvotes: 7