c# - CodeContracts: How to fullfill Require in Ctor using this() call? -
I am playing with Microsoft's codecontracts and I had to face a problem that I could not solve Was there. I have got a class with two constructors:
public fu (public float F) {contract. Requirement (f> 0); } Public Fu (int i): this (float) i) {}
The example has been simplified. I do not know what to do for the second constructor's f
0. Is it possible with contracts?
You can simply add pre-condition to the body of another constructor.
Public Test Class (float F) {contract. Requirements (f> 0); New exception throw ("foo"); } Public Test Class (Int I.): ((Float) i) {Agreement. Requirements (i> gt; 0); }
Edit
Try calling the above code:
TestClass test2 = New Test Class ((integer -1);
You will see that the pre-condition is thrown before throwing a regular exception.
Comments
Post a Comment