Good morning! I am writing a class for drawing histograms, and for the convenience of users, I decided to add several convenience constructors.
However, as soon as I recently switched to a .NET contract with DevLabs, I want to fully use the preconditions to protect against my (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) {
Something bothers me. I do not want the first constructor to ever name the second if the contract was broken; however, it is assumed that a call to this(...) will be performed before the body of the first constructor is executed.
Will this code work the way I want? I have not tried it yet. And if not, is there any way to solve this problem?
wh1t3cat1k
source share