Good morning! I'm writing a class for drawing histograms and, for user's convenience, I've decided to add a few convenience constructors.
However, as soon as I recently switched to .NET code contracts from DevLabs, I want to make full use of preconditions for protection against my own (or someone's) stupidity.
public HistoGrapher(IList<string> points, IList<T> values)
: this(points.Select((point, pointIndex) => new KeyValuePair<string, T>(point, values[pointIndex])))
{
Contract.Requires<ArgumentNullException>(points != null, "points");
Contract.Requires<ArgumentNullException>(values != null, "values");
Contract.Requires<ArgumentException>(points.Count == values.Count, "The lengths of the lists should be equal.");
}
public HistoGrapher(IEnumerable<KeyValuePair<string, T>> pointValuePairs)
{
// useful code goes here
}
There is a thing that confuses me. I don't want the first constructor to ever call the second one if the contract is broken; however, it is supposed that a call to this(...)
will be performed before executing the body of the first constructor.
Will this code work as I want? I haven't tried yet. And if not, is there a capability of solving such an issue?