Code Contracts ForumDiscuss and provide feedback on Code Contracts.© 2009 Microsoft Corporation. All rights reserved.Wed, 25 Nov 2009 19:50:48 Z971f4c89-b711-4609-9d6e-87af0021bd2fhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/7dfdf7ec-3d27-40c5-87d8-9a3c98a6199ehttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/7dfdf7ec-3d27-40c5-87d8-9a3c98a6199eAlexey R.http://social.msdn.microsoft.com/Profile/en-US/?user=Alexey%20R.Why inferring methodensures doesn't work here?<p>Compiled with option &quot;-infer methodensures&quot;:<br/><br/>    class Program<br/>    {<br/>        static void Main(string[] args)<br/>        {<br/>            string s = Test();<br/>            Contract.Assert(s.Length &lt; 10); // assert unproven<br/>        }</p> <p>        static string Test()<br/>        {<br/>            string s = new string('x', 7);<br/>            Contract.Assume(s.Length &lt; 10);<br/>            return s;<br/>        }<br/>    }</p>Wed, 25 Nov 2009 19:50:46 Z2009-11-25T19:50:48Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/78402746-0a13-4127-bb55-e7b54f1786fdhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/78402746-0a13-4127-bb55-e7b54f1786fdJonathan Allenhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jonathan%20AllenFalse Positive: CodeContracts: Suggested precondition: Contract.Requires(!string.IsNullOrEmpty(value));<pre lang=x-vbnet><br/> &lt;Extension()&gt; Public Function IsEmailAddress(ByVal value As String) As Boolean If value = &quot;&quot; Then Return False Try Dim temp = New MailAddress(value) Return True Catch ex As FormatException Return False End Try End Function</pre>Sat, 21 Nov 2009 07:53:10 Z2009-11-25T19:13:44Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/72e02d49-5633-4e6a-8657-4280c98a7aa5http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/72e02d49-5633-4e6a-8657-4280c98a7aa5Alexey R.http://social.msdn.microsoft.com/Profile/en-US/?user=Alexey%20R.ContractsRuntime recursion guard optimization<p>The recursion guard is not necessary if contract statements doesn't call any methods, for example:<br/><br/>    <span style="color:#1000a0">if</span> (<a title="System.Diagnostics.Contracts.__ContractsRuntime" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Player/System.Diagnostics.Contracts.__ContractsRuntime">__ContractsRuntime</a>.<a title="int System.Diagnostics.Contracts.__ContractsRuntime.insideContractEvaluation;" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Player/System.Diagnostics.Contracts.__ContractsRuntime/insideContractEvaluation:Int32">insideContractEvaluation</a> &lt;= <span style="color:#800000">4</span>)<br/>    {<br/>        <span style="color:#1000a0">try</span><br/>        {<br/>            <a title="System.Diagnostics.Contracts.__ContractsRuntime" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Player/System.Diagnostics.Contracts.__ContractsRuntime">__ContractsRuntime</a>.<a title="int System.Diagnostics.Contracts.__ContractsRuntime.insideContractEvaluation;" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Player/System.Diagnostics.Contracts.__ContractsRuntime/insideContractEvaluation:Int32">insideContractEvaluation</a>++;<br/>            <a title="System.Diagnostics.Contracts.__ContractsRuntime" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Player/System.Diagnostics.Contracts.__ContractsRuntime">__ContractsRuntime</a>.<a title="void System.Diagnostics.Contracts.__ContractsRuntime.Requires(bool condition, string message, string conditionText);" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Player/System.Diagnostics.Contracts.__ContractsRuntime/Requires(Boolean,String,String)">Requires</a>(<a title="int length; // Parameter">arg1</a> &gt;= <span style="color:#800000">0</span>, <span style="color:#800000">null</span>, <span style="color:#800000">&quot;arg1 &gt;= 0&quot;</span>);<br/>            <a title="System.Diagnostics.Contracts.__ContractsRuntime" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Player/System.Diagnostics.Contracts.__ContractsRuntime">__ContractsRuntime</a>.<a title="void System.Diagnostics.Contracts.__ContractsRuntime.Requires(bool condition, string message, string conditionText);" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Player/System.Diagnostics.Contracts.__ContractsRuntime/Requires(Boolean,String,String)">Requires</a>(<a title="int length; // Parameter">arg1</a> &lt;= <span style="color:#800000">127</span>, <span style="color:#800000">null</span>, <span style="color:#800000">&quot;arg1 &lt;= 127&quot;</span>);<br/>        }<br/>        <span style="color:#1000a0">finally</span><br/>        {<br/>            <a title="System.Diagnostics.Contracts.__ContractsRuntime" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Player/System.Diagnostics.Contracts.__ContractsRuntime">__ContractsRuntime</a>.<a title="int System.Diagnostics.Contracts.__ContractsRuntime.insideContractEvaluation;" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Player/System.Diagnostics.Contracts.__ContractsRuntime/insideContractEvaluation:Int32">insideContractEvaluation</a>--;<br/>        }<br/>    }<br/><br/>It will be ok to just leave __ContractRuntime.Requires here:<br/><br/>            <a title="System.Diagnostics.Contracts.__ContractsRuntime" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Player/System.Diagnostics.Contracts.__ContractsRuntime">__ContractsRuntime</a>.<a title="void System.Diagnostics.Contracts.__ContractsRuntime.Requires(bool condition, string message, string conditionText);" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Player/System.Diagnostics.Contracts.__ContractsRuntime/Requires(Boolean,String,String)">Requires</a>(<a title="int length; // Parameter">arg1</a> &gt;= <span style="color:#800000">0</span>, <span style="color:#800000">null</span>, <span style="color:#800000">&quot;arg1 &gt;= 0&quot;</span>);<br/>            <a title="System.Diagnostics.Contracts.__ContractsRuntime" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Player/System.Diagnostics.Contracts.__ContractsRuntime">__ContractsRuntime</a>.<a title="void System.Diagnostics.Contracts.__ContractsRuntime.Requires(bool condition, string message, string conditionText);" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://Player/System.Diagnostics.Contracts.__ContractsRuntime/Requires(Boolean,String,String)">Requires</a>(<a title="int length; // Parameter">arg1</a> &lt;= <span style="color:#800000">127</span>, <span style="color:#800000">null</span>, <span style="color:#800000">&quot;arg1 &lt;= 127&quot;</span>);<br/><br/>Runtime contract checking is not for debugging only and would be nice not to have any unnecessary overhead in deployed code. There is no such guard in constructors already. Maybe it's not difficult to slightly optimize other methods where the guard is not required.</p>Sun, 15 Nov 2009 12:20:04 Z2009-11-25T18:31:19Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/d1421890-aac0-43f9-bd9d-c3c074233f50http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/d1421890-aac0-43f9-bd9d-c3c074233f50Jonathan Allenhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jonathan%20AllenFalse positive on LinkedList.FirstThis block warns that m_List.First may return a null.<br/><br/><br/> <pre lang=x-vbnet>Private ReadOnly m_List As New LinkedList(Of T) Public Function TryDequeue(ByRef value As T) As Boolean SyncLock m_Lock If m_List.Count &gt; 0 Then value = m_List.First.Value m_List.RemoveFirst() Return True Else value = Nothing Return False End If End SyncLock End Function</pre> <br/><br/>Wed, 25 Nov 2009 03:40:36 Z2009-11-25T18:37:15Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/ea3e9ec4-661c-4b19-80a5-92cd7fa06864http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/ea3e9ec4-661c-4b19-80a5-92cd7fa06864Jonathan Allenhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jonathan%20AllenFalse Positive on read-only fieldsThe field is declared as shown below. Whenever I try to read from that field, I get a &quot;possibly calling a method on a null reference&quot; warning.<br/><br/><span style="font-family:Consolas;color:#0000ff;font-size:x-small"><span style="font-family:Consolas;color:#0000ff;font-size:x-small"><span style="font-family:Consolas;color:#0000ff;font-size:x-small"><font face=Consolas size=2 color="#0000ff"><font face=Consolas size=2 color="#0000ff"><font face=Consolas size=2 color="#0000ff"> <p>Private</p> </font></font></font></span><font face=Consolas size=2 color="#0000ff"><font face=Consolas size=2 color="#0000ff"> <p> </p> </font></font></span><font face=Consolas size=2 color="#0000ff"> <p> </p> </font></span> <p><span style="font-family:Consolas;font-size:x-small"><span style="font-family:Consolas;font-size:x-small"> </span></span><span style="font-family:Consolas;color:#0000ff;font-size:x-small"><span style="font-family:Consolas;color:#0000ff;font-size:x-small"><span style="font-family:Consolas;color:#0000ff;font-size:x-small">ReadOnly</span></span></span><span style="font-family:Consolas;font-size:x-small"><span style="font-family:Consolas;font-size:x-small"> m_Query </span></span><span style="font-family:Consolas;color:#0000ff;font-size:x-small"><span style="font-family:Consolas;color:#0000ff;font-size:x-small"><span style="font-family:Consolas;color:#0000ff;font-size:x-small">As</span></span></span><span style="font-family:Consolas;font-size:x-small"><span style="font-family:Consolas;font-size:x-small"> </span></span><span style="font-family:Consolas;color:#0000ff;font-size:x-small"><span style="font-family:Consolas;color:#0000ff;font-size:x-small"><span style="font-family:Consolas;color:#0000ff;font-size:x-small">New</span></span></span><span style="font-family:Consolas;font-size:x-small"><span style="font-family:Consolas;font-size:x-small"> </span></span><span style="font-family:Consolas;color:#2b91af;font-size:x-small"><span style="font-family:Consolas;color:#2b91af;font-size:x-small"><span style="font-family:Consolas;color:#2b91af;font-size:x-small">QueryParameterCollection</span></span></span></p>Wed, 25 Nov 2009 03:33:29 Z2009-11-25T14:49:26Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/19ec6425-7bef-4c8b-8c65-72f0152ac026http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/19ec6425-7bef-4c8b-8c65-72f0152ac026Kithttp://social.msdn.microsoft.com/Profile/en-US/?user=KitInstaller path issueI just installed V1.2.21023.14 on top of VS2010 beta 2 on windows 7 x64. The file<br/>C:\Program Files (x86)\MSBuild\<strong>4.0</strong>\Microsoft.Common.targets\ImportAfter\CodeContracts.targets<br/>was installed at the wrong location - it should have been <br/>C:\Program Files (x86)\MSBuild\<strong>v4.0</strong>\Microsoft.Common.targets\ImportAfter\CodeContracts.targets<br/>Because of this it was impossisble to create projects in VS.<br/>Hope this helps .Wed, 25 Nov 2009 10:12:19 Z2009-11-25T10:12:19Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/8e25c07f-d080-46a6-82b4-c88595cd0adchttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/8e25c07f-d080-46a6-82b4-c88595cd0adcJonathan Allenhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jonathan%20AllenUnclear Warning "CodeContracts: requires unproven"Often I see this warning with no explaination as to what requirement isn't proven.Wed, 25 Nov 2009 03:54:12 Z2009-11-25T06:08:01Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/2fb75495-e41b-4fdd-9ebb-ba11891a4367http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/2fb75495-e41b-4fdd-9ebb-ba11891a4367Dave Sextonhttp://social.msdn.microsoft.com/Profile/en-US/?user=Dave%20SextonAutomatic Non Null Invariant for Initialized readonly Field in C#Hi, <br/><br/>Why isn't there an automatic assumption by CC that initialized readonly fields in C# will never be null?<br/><br/> <pre lang="x-c#">namespace TestContracts { class Program { static void Main(string[] args) { } readonly string value = &quot;Hello&quot;; int Length { get { return value.Length; } } } } </pre> <br/><br/>  Message 1 CodeContracts: Suggested precondition: Contract.Requires(this.value != null); [file] 15 5 TestContracts<br/>  Warning 2 CodeContracts: Possibly calling a method on a null reference 'this.value' [file] 15 5 TestContracts<br/>  Message 3 CodeContracts: Checked 5 assertions: 4 correct 1 unknown  [program] 1 1 TestContracts<br/><br/><br/>I understand that even readonly fields can be mutated via reflection, but I don't think that possibility should apply for CC static checking.  After all, even if I explicitly specify an object invariant to silence the static checker, the field could still be set to null by reflection at runtime, which means that the invariant is no longer true and all of the checking performed against it at compile time may be invalid at runtime.<br/><br/>Or is there some other subtle technical reason perhaps?<br/><br/>Thanks, <br/>Dave<hr class="sig">http://davesexton.com/blogTue, 24 Nov 2009 20:35:13 Z2009-11-25T00:09:43Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/38b6a841-d292-4a55-8237-a2c9f85639a8http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/38b6a841-d292-4a55-8237-a2c9f85639a8Alexey R.http://social.msdn.microsoft.com/Profile/en-US/?user=Alexey%20R.Inferring invariantsI tried to switch on inferring ensures and requires for internal (non-public) methods. This greatly reduces amount code required to pass the static checking. And much more code could be cleaned if we had an option to infer invariants, at least for readonly fields, which are assigned in one place only (in ctor):<br/><br/>class Example<br/>{<br/>  readonly int propA;<br/>  readonly int[] items;<br/><br/>  public Example(int a, int count)<br/>  {<br/>     Contract.Requires(a &gt;= 0);<br/>     Contract.Requires(count &gt;= 0);<br/><br/>     this.propA = a;<br/>     this.items = new int[count];<br/>  }<br/><br/>  [ContractInvariantMethod]<br/>  void Invariant()<br/>  {<br/>     // usually many of the statements here could be simply inferred from ctor<br/>     // but we have to write them explicitly. And many of them unnecessarily<br/>     // reevaluated at each public method call (if we enable runtime checking)<br/><br/>     Contract.Invariant(this.propA &gt;= 0); // can be inferred from ctor, never changes during object lifetime<br/>     Contract.Invariant(this.items != null); // can be inferred from ctor, never changes during object lifetime<br/>     ....<br/>  }<br/>}<br/>Tue, 24 Nov 2009 19:53:28 Z2009-11-25T00:24:33Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/af046685-3061-4502-b1c2-0a5ad96796c3http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/af046685-3061-4502-b1c2-0a5ad96796c3Jahn Otto Næsgaard Andersenhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jahn%20Otto%20N%u00e6sgaard%20AndersenPossible bug?I have found a possible bug in the static checker.<br/> <br/> I have two functions A and B with the following postconditions:<br/> <br/> public float A()<br/> {<br/> Contract.Ensures(Contract.Result&lt;float&gt;() &gt;= 0);<br/> ...<br/> }<br/> <br/> public float B()<br/> {<br/> Contract.Ensures(Contract.Result&lt;float&gt;() &gt; 0);<br/> ...<br/> }<br/> <br/> and a function C with the following precondition:<br/> <br/> public void C(float param)<br/> {<br/> Contract.Requires(param &gt;= 0);<br/> ...<br/> }<br/> <br/> I'm calling C with<br/> <br/> C(A() * B());<br/> <br/> As A guarantees a result &gt;= 0 and B guarantees &gt; 0, I would expect A*B to be &gt;= 0. However, the static checker complains that C's precondition is not met.<br/> <br/> However, if I change the B postcondition to be Contract.Ensures(Contract.Result&lt;float&gt;() &gt; 0), it works.<br/>Mon, 23 Nov 2009 14:18:33 Z2009-11-24T13:31:00Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/f8064b57-0a00-48d2-a68e-6a019c3600d8http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/f8064b57-0a00-48d2-a68e-6a019c3600d8DaveChanhttp://social.msdn.microsoft.com/Profile/en-US/?user=DaveChanIs it fixed in latest release about 'Call to impure method in a pure region in method'I got the same error information in my app.<br/> <br/> I made some code like <br/> <br/> <pre lang="x-c#">public static bool Update(IClub club) { Contract.Requires(club != null, ClubMessages.Required); Contract.Requires(MyDomain.Clubs().Any(i =&gt; i.Number == club.Number), ClubMessages.NotExists); return MyDomain.DataContext().Update(club); } </pre> <br/> <br/> An warning will be thrown when it's building.<br/> <br/> <pre>Detected call to impure method 'System.Linq.Expressions.Expression.Field(System.Linq.Expressions.Expression,System.Reflection.FieldInfo)' in a pure region in method 'MyDomain.AddClub(MyProject.BusinessModels.IClub)'. </pre> <br/> <br/> I try to refactor the assertion to a separated method as a basic query.<br/> <br/> <pre lang="x-c#"> public static bool IsExists(this IClub club) { Contract.Requires(club != null,ClubMessages.Required); return MyDomain.Clubs().Any(i =&gt; i.Number == club.Number); } public static bool Update(IClub club) { Contract.Requires(club != null, ClubMessages.Required); Contract.Requires(club.IsExists(), ClubMessages.NotExists); return MyDomain.DataContext().Update(club); } </pre> <br/> <br/> But the warning is still there. it says<br/> <br/> <pre lang="x-c#">Detected call to impure method 'MyProject.BusinessModels.MyDomain.IsExists(MyProject.BusinessModels.IClub)' in a pure region in method 'MyProject.BusinessModels.MyDomain.Update(MyProject.BusinessModels.IClub)' </pre> <br/> <br/> searched it in google, and got the post  <h3 class=r><a href="http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/3dfb5ff2-1a44-4534-a243-a9e02b5e61e8">Call to impure <em>method</em> in <em>a pure region in method</em> </a></h3> <br/> It says it's a known issue, and will be fixed in new release.<br/> <br/> But i still get the same warning information under vs2010 b2 and <a title="Code Contracts" href="codecontracts/threads" title="Code Contracts">Code Contracts 1.2.21023.14.</a> <br/> <br/> Anybody can give me some information about it?<br/> <br/> Thanks at advance.<br/> <br/>Tue, 24 Nov 2009 12:45:31 Z2009-11-24T12:45:32Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/1231e5e2-da9d-4b29-9091-5df51dfbf66dhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/1231e5e2-da9d-4b29-9091-5df51dfbf66dJohn Melvillehttp://social.msdn.microsoft.com/Profile/en-US/?user=John%20MelvilleIs the contract for stream.read incorrect?The contract for Stream.Read in MSCorLib.Contracts contains the following contract: (via reflector)<br/><br/>    <a title=System.Diagnostics.Contracts.Contract href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib.Contracts::188286aac86319f9/System.Diagnostics.Contracts.Contract">Contract</a>.<a title="void System.Diagnostics.Contracts.Contract.Requires(bool condition, string userSuppliedString, string sourceText);" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib.Contracts::188286aac86319f9/System.Diagnostics.Contracts.Contract/Requires(Boolean,String,String)">Requires</a>((<a title=System.Boolean href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib::b77a5c561934e089/System.Boolean">bool</a>) ((<a title="int offset; // Parameter">offset</a> + <a title="int count; // Parameter">count</a>) &lt; <a title="byte[] buffer; // Parameter">buffer</a>.<a title="int System.Array.Length { ... }" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib.Contracts::188286aac86319f9/System.Array/property:Length:System.Int32">Length</a>), <span style="color:#800000">null</span>, <span style="color:#800000">&quot;offset + count &lt; buffer.Length&quot;</span>);<br/><br/>this excludes the following (legal code)<br/>   var buffer = new byte[10];<br/>   var s = /* some stream */<br/>   s.Read(buffer, 0, 10);<br/><br/>This appears to be a bug,  I believe the contract (and several related contracts in that same class including beginread and write) should read:<br/>    <a title=System.Diagnostics.Contracts.Contract href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib.Contracts::188286aac86319f9/System.Diagnostics.Contracts.Contract">Contract</a>.<a title="void System.Diagnostics.Contracts.Contract.Requires(bool condition, string userSuppliedString, string sourceText);" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib.Contracts::188286aac86319f9/System.Diagnostics.Contracts.Contract/Requires(Boolean,String,String)">Requires</a>((<a title=System.Boolean href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib::b77a5c561934e089/System.Boolean">bool</a>) ((<a title="int offset; // Parameter">offset</a> + <a title="int count; // Parameter">count</a>) &lt;= <a title="byte[] buffer; // Parameter">buffer</a>.<a title="int System.Array.Length { ... }" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib.Contracts::188286aac86319f9/System.Array/property:Length:System.Int32">Length</a>), <span style="color:#800000">null</span>, <span style="color:#800000">&quot;offset + count &lt; buffer.Length&quot;</span>);<br/><br/>Thanks<br/><br/>John MelvilleMon, 23 Nov 2009 05:14:27 Z2009-11-24T11:43:34Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/39bd87dd-670a-44b4-b792-77898248cdefhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/39bd87dd-670a-44b4-b792-77898248cdefDave Sextonhttp://social.msdn.microsoft.com/Profile/en-US/?user=Dave%20SextonBinaryFormatter Deserialize Missing Ensures Return Not NullHi, <br/><br/>Could the <strong>BinaryFormatter.Deserialize</strong> method have a contract that ensures the return value will never be null?<br/><br/>Thanks, <br/>Dave<hr class="sig">http://davesexton.com/blogTue, 24 Nov 2009 11:03:10 Z2009-11-24T11:03:11Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/edad1ccc-bebb-43c5-bff1-d07d25033977http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/edad1ccc-bebb-43c5-bff1-d07d25033977Dave Sextonhttp://social.msdn.microsoft.com/Profile/en-US/?user=Dave%20SextonContract.ForAll Static CheckingHi, <br/><br/>Any word yet on the status of supporting <strong>Contract.ForAll</strong> in the static checker?<br/><br/>I'm experiencing the same issue as in the following post using the types built-in to Beta 2.<br/><br/><a href="http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/94ec2bce-61b5-463c-a1b6-22c22a42e67f">http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/94ec2bce-61b5-463c-a1b6-22c22a42e67f</a> <br/><br/>Thanks, <br/>Dave<hr class="sig">http://davesexton.com/blogTue, 24 Nov 2009 10:53:24 Z2009-11-24T10:53:25Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/3668d361-beaf-424f-9138-6485a5116b32http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/3668d361-beaf-424f-9138-6485a5116b32Dave Sextonhttp://social.msdn.microsoft.com/Profile/en-US/?user=Dave%20SextonSuggested precondition for null check ignores existing precondition for String.IsNullOrWhiteSpace<p>Hi, <br/><br/>CC seems to ignore the fact that String.<strong>IsNullOrWhiteSpace</strong> checks for null, yet the same fact for String.IsNullOrEmpty is not ignored.<br/><br/>For example, the following code: <br/></p> <pre lang="x-c#">using System.Diagnostics.Contracts; namespace TestContracts { class Program { static void Main(string[] args) { CallFoo(&quot;text&quot;, &quot;text2&quot;); } static void CallFoo(string text, string text2) { Contract.Requires(!string.IsNullOrEmpty(text)); Contract.Requires(!string.IsNullOrWhiteSpace(text2)); Foo(text); Foo(text2); } static void Foo(string text) { Contract.Requires(text != null); } } } </pre> <p>Produces the following warnings with static checking enabled: <br/><br/>  Message 1 CodeContracts: Suggested precondition: Contract.Requires(text2 != null); [file] 17 4 TestContracts<br/>  Warning 2 CodeContracts: requires unproven [file] 9 4 TestContracts<br/>  Warning 3   + location related to previous warning [file] 15 4 TestContracts<br/>  Warning 4 CodeContracts: requires unproven [file] 18 4 TestContracts<br/>  Warning 5   + location related to previous warning [file] 23 4 TestContracts<br/>  Message 6 CodeContracts: Checked 6 assertions: 4 correct 2 unknown  [program] 1 1 TestContracts<br/><br/><br/>Am I correct in expecting that there should be no suggestions or warnings at all for this program?<br/><br/>Thanks, <br/>Dave</p><hr class="sig">http://davesexton.com/blogTue, 24 Nov 2009 10:21:32 Z2009-11-24T10:43:38Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/670ad8a6-f1a8-4343-a413-fd566c252cc5http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/670ad8a6-f1a8-4343-a413-fd566c252cc5Alexey R.http://social.msdn.microsoft.com/Profile/en-US/?user=Alexey%20R.Missing Requires in indexer of ReadOnlyCollection<T><p>class MyCollection : ReadOnlyCollection&lt;object&gt;<br/>{<br/>    public MyCollection()<br/>       : base(new object[0])<br/>    {<br/>    }</p> <p>    public object GetItem()<br/>    {<br/>       return this[-1];<br/>    }<br/>}<br/><span style="font-family:Consolas;font-size:xx-small"><span style="font-family:Consolas;font-size:xx-small"><br/>CodeContracts: Checked 1 assertion: 1 correct</span></span></p>Wed, 18 Nov 2009 18:43:36 Z2009-11-22T15:21:04Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/e79e198b-bc2a-4e8a-a9a2-8272c7c3a7e9http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/e79e198b-bc2a-4e8a-a9a2-8272c7c3a7e9Reishttp://social.msdn.microsoft.com/Profile/en-US/?user=ReisCan this be used with the Compact Framework?Will code contracts work on Windows CE running the Compact 3.5 Framework?<br/><br/>Thanks in advanceSun, 15 Nov 2009 19:28:00 Z2009-11-22T14:41:00Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/bc627bd3-8025-4872-aa57-02f52b8fbbf7http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/bc627bd3-8025-4872-aa57-02f52b8fbbf7xor88http://social.msdn.microsoft.com/Profile/en-US/?user=xor88Suggestion: Contract.Requires(bool condition, Func<string> messageGenerator)Reason: you might not want the message to be evaluated in all error free cases. It might be expensive.Sat, 21 Nov 2009 01:21:07 Z2009-11-21T06:54:05Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/db735733-9eba-4123-8f5b-e729b307063fhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/db735733-9eba-4123-8f5b-e729b307063fStrilanchttp://social.msdn.microsoft.com/Profile/en-US/?user=StrilancContract Request by Usage - This method's contracts should be verifiably correctI have the following, relatively complicated, method to split text into lines less than a maximum size:<br/> <br/> <pre lang=x-vbnet> &lt;Pure()&gt; Public Function SplitText(ByVal body As String, ByVal maxLineLength As Integer) As IList(Of String) Contract.Requires(body IsNot Nothing) Contract.Requires(maxLineLength &gt; 0) Contract.Ensures(Contract.Result(Of IList(Of String))() IsNot Nothing) Contract.Ensures(Contract.Result(Of IList(Of String))().Count &gt; 0) Dim result = New List(Of String)() For Each line In Microsoft.VisualBasic.Split(body, Delimiter:=Environment.NewLine) If line.Length &lt;= maxLineLength Then result.Add(line) Continue For End If Dim subline = New System.Text.StringBuilder(capacity:=maxLineLength) Contract.Assert(subline.Length = 0) For Each word In line.Split(&quot; &quot;c) Contract.Assert(subline.Length &lt;= maxLineLength) If word.Length &gt; maxLineLength Then 'word won't fit on its own line; jam it in! 'dump current line If subline.Length &gt; 0 Then Dim n = maxLineLength - subline.Length - 1 If n &gt; 0 Then subline.Append(&quot; &quot;) subline.Append(word.Substring(0, n)) word = word.Substring(n) End If result.Add(subline.ToString) subline.Clear() End If Contract.Assert(subline.Length = 0) 'dump full lines worth of the word Dim p = 0 For p = 0 To word.Length - 1 - maxLineLength Step maxLineLength Contract.Assert(p + maxLineLength &lt;= word.Length) result.Add(word.Substring(p, maxLineLength)) Next p 'continue with remaining portion of the word word = word.Substring(p) Contract.Assert(word.Length &lt;= maxLineLength) Contract.Assert(subline.Length = 0) ElseIf subline.Length = 0 Then 'first word on new line 'no action needed, we already know the word will fit ElseIf subline.Length + 1 + word.Length &lt;= maxLineLength Then 'word will fit subline.Append(&quot; &quot;) 'separate from previous word Else 'word won't fit on current line, start new line result.Add(subline.ToString) subline.Clear() End If Contract.Assert(subline.Length + word.Length &lt;= maxLineLength) subline.Append(word) Contract.Assert(subline.Length &lt;= maxLineLength) Next word Contract.Assert(result.Count &gt; 0 OrElse subline.Length &gt; 0) 'Dump last line If subline.Length &gt; 0 Then result.Add(subline.ToString) End If Contract.Assert(result.Count &gt; 0) Next line Return result End Function </pre> Contracts 1.2.21023.14 gives the following warnings:<br/> - Couldn't prove result.count &gt; 0 [probably because there are no contracts on the vb variant of the Split function]<br/> - Couldn't prove a bunch of assertions involving subline.length [probably there are no, or not enough, contracts on Text.StringBuilder]<br/> - Couldn't prove word.length &lt;= maxLineLength [probably because the verifier doesn't understand the for-loop well enough]Fri, 20 Nov 2009 18:11:57 Z2009-11-20T19:40:11Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/69c41d02-1a33-4f5a-a32c-16fb89b41b36http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/69c41d02-1a33-4f5a-a32c-16fb89b41b36fcharlonhttp://social.msdn.microsoft.com/Profile/en-US/?user=fcharlonStatic checker fails to identify obvious problemsHi,<br/>I'm currently experimenting code contract, so I wrote this very simple piece of code:<br/> <pre lang="x-c#"> public class Test { private List&lt;int&gt; list; public Test() { list = new List&lt;int&gt;(); } public List&lt;int&gt; List { get { return list; } } [ContractInvariantMethod] protected void ObjectInvariant() { Contract.Invariant(list.Count == 0); } }</pre> The static checker doesn't see any problem with that code, although I can obviously do that and break the invariant:<br/><br/> <pre lang="x-c#">Test t = new Test(); t.List.Add(1);</pre> Can someone shed some light on that?<br/><br/>ThanksFri, 13 Nov 2009 12:47:52 Z2009-11-20T13:42:41Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/5a15b99d-7d66-46bd-9227-9df644e9ee7chttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/5a15b99d-7d66-46bd-9227-9df644e9ee7cJahn Otto Næsgaard Andersenhttp://social.msdn.microsoft.com/Profile/en-US/?user=Jahn%20Otto%20N%u00e6sgaard%20AndersenMore trouble with mixed-mode projectsI have a C# project that references a mixed-mode C++/CLI assembly in the same solution. When I'm enabling static checking on this, I'm getting the following warning:<br/> <br/> Warning    71    Contract reference assembly for project 'MyMixedModeProject' not found. Select 'Build' or 'DoNotBuild' for Contract Reference in project settings.    MyManagedProject<br/> <br/> How can I get rid of this warning? There is no &quot;code contracts&quot; tab in the mixed mode project. Quite often this causes the static checker to give up.<br/>Thu, 19 Nov 2009 15:15:50 Z2009-11-20T08:38:37Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/77701d98-9006-43d4-a0b8-4741d6971fe8http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/77701d98-9006-43d4-a0b8-4741d6971fe8Strilanchttp://social.msdn.microsoft.com/Profile/en-US/?user=StrilancContract Requests - Net.IPEndPoint, Net.IPAddress, and a new postcondition for String.IndexOfPlease add contracts to Net.IPEndPoint and Net.IPAddress. Eg. GetAddressBytes() != null<br/> <br/> String.IndexOf should have the following postcondition:<br/> result == -1 || result &lt;= this.length - value.length<br/> <br/> So that, for example, this code will have no warnings on the SubString line:<br/> <pre lang=x-vbnet>Public Module M Public Function TryGetTail(ByVal value As String, ByVal divider As String) As String Contract.Requires(value IsNot Nothing) Contract.Requires(divider IsNot Nothing) Dim p = value.IndexOf(divider) If p = -1 Then Return Nothing Return value.Substring(p + divider.Length) End Function End Module </pre>Fri, 20 Nov 2009 03:41:51 Z2009-11-20T03:41:51Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/aba1e662-9e5f-44ba-88bc-be204a0aaeb9http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/aba1e662-9e5f-44ba-88bc-be204a0aaeb9David K Allenhttp://social.msdn.microsoft.com/Profile/en-US/?user=David%20K%20AllenRewrite failed to produce verifiable assemblyI discovered that if you have runtime checking enabled for a project, and there are no code contracts in it, you will get a compile error &quot;Rewrite failed to produce verifiable assembly&quot;.  Knowing this now, it's easy to resolve. But it can sometimes take a while to figure this out. It came up because I was writing a tutorial on Code Contracts, and I wanted to start the user with a code base without code contracts, but with contracts enabled so they could focus on the basics.<br/><br/>It's not a big deal, but if you can make it a friendlier experience, it may help reduce frustration for novices. In the meantime, for my exercises, I merely add a dummy class with a code contract, and all is well.  I may change that to require them to enable the setting themselves. After all, it is a tutorial.<hr class="sig">Best Regards, David K Allen Minneapolis, Minnesota, USATue, 17 Nov 2009 05:04:57 Z2009-11-20T00:06:51Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/cd1d71c9-9e4f-43b0-93bd-8cd8883c3c24http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/cd1d71c9-9e4f-43b0-93bd-8cd8883c3c24Kathleen Dollardhttp://social.msdn.microsoft.com/Profile/en-US/?user=Kathleen%20DollardSetting Runtime Checking in VS 2010 Beta 2 Team<p>I had Code Contracts in place which worked - displaying a dialog on failure. <br/><br/>Today, they do not work. They are simply overlooked even though they should be failing.<br/><br/>I assume that something happened which resulted in runtime checking being turned off.<br/><br/>So, I tried to find where to turn runtime checking back on, and I do not have a Code Contracts tab in any project properties dialog, this project or a new project. <br/><br/>Ideas as to why the tab is missing, a mechanism for retrieving it, guesses as to why runtime checking was turned off (without my involvement), another way to turn runtime checking, or any other thoughts.</p>Tue, 17 Nov 2009 16:00:18 Z2009-11-19T18:38:24Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/59e86aba-6b8a-4f60-912e-25a1873667dfhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/59e86aba-6b8a-4f60-912e-25a1873667dfAlexey R.http://social.msdn.microsoft.com/Profile/en-US/?user=Alexey%20R.One more variation of Assert/AssumeSuppose we want to deploy some code dll with runtime checking enabled. Desired checking level == Preconditions.<br/><br/>I would suggest to add one more variant of Assert/Assume (Contract.Verify() for examaple), which will behave exactly like Assume, but will remain in assembly if we compile with checking levels != Full. This is how we might use it:<br/><br/> <div style="background-color:white;color:black">    void Test()<br/>    {<br/>        int x = CallExternalLib(); // called method has no contracts</div> <div style="background-color:white;color:black"><br/>        // we only need this Assume to satisfy the static checker,<br/>        // we don't want this check to stay in production dll<br/>        // because the condition will be checked within DoSomething()<br/>        // which has Contract.Requires(x &gt;= 0)<br/><br/>        Contract.Assume(x &gt;= 0);<br/><br/></div> <div style="background-color:white;color:black">        bool success = DoSomething(x); // requires x &gt;= 0<br/><br/></div> <div style="background-color:white;color:black">        // but we want this check to stay in production dll,<br/>        // because the result of DoSomething cannot be statically proven<br/>        Contract.Assume(success);<br/><br/>        // suggested alternaltive (stays in assembly even when checking level != Full)<br/>        // Contract.Verify(success)<br/>    }<br/><br/></div>Thu, 19 Nov 2009 11:09:52 Z2009-11-19T11:09:53Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/3dbe414f-5970-4c47-a5e7-b237f8bbd146http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/3dbe414f-5970-4c47-a5e7-b237f8bbd146Porgeshttp://social.msdn.microsoft.com/Profile/en-US/?user=PorgesAdding ensures causes other contracts to failOk, I have the following: <div><span style="background-color:#ffffff"><br/></span></div> <div> <div style="color:Black;background-color:White"> <pre> [Pure] <span style="color:Blue">public</span> <span style="color:Blue">override</span> <span style="color:Blue">string</span> NodeName { <span style="color:Blue">get</span> { <span style="color:Blue">return</span> <span style="color:#A31515">&quot;#comment&quot;</span>; } } </pre> </div> All my contracts are proven by the static checker. However, when I change it to this:</div> <div><br/></div> <div> <div style="color:Black;background-color:White"> <pre> [Pure] <span style="color:Blue">public</span> <span style="color:Blue">override</span> <span style="color:Blue">string</span> NodeName { <span style="color:Blue">get</span> { Contract.Ensures(Contract.Result&lt;<span style="color:Blue">string</span>&gt;() == <span style="color:#A31515">&quot;#comment&quot;</span>); <span style="color:Blue">return</span> <span style="color:#A31515">&quot;#comment&quot;</span>; } } </pre> </div> I get other (apparently unrelated) contracts failing. The point of failure is the 'return' line.</div> <div><br/></div> <div>The contracts in question are on the superclass of this, in an invariant:</div> <div> <div style="color:Black;background-color:White"> <pre> [ContractInvariantMethod] <span style="color:Blue">protected</span> <span style="color:Blue">new</span> <span style="color:Blue">void</span> ObjectInvariant() { Contract.Invariant(dataField != <span style="color:Blue">null</span>); Contract.Invariant(TextContent == dataField); <span style="color:Green">// fails</span> Contract.Invariant(NodeValue == dataField); <span style="color:Green">// fails</span> Contract.Invariant(Data == dataField); } </pre> </div> Is this a bug in Contracts, or am I doing something wrong?</div> <div><br/></div> <div>I can supply full source if that will help.</div>Sun, 08 Nov 2009 04:35:35 Z2009-11-18T17:02:37Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/8ebc6ea0-f5e6-4bcb-be85-7385d36b6afbhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/8ebc6ea0-f5e6-4bcb-be85-7385d36b6afbKathleen Dollardhttp://social.msdn.microsoft.com/Profile/en-US/?user=Kathleen%20DollardDefault Display Hides Abort/Retry/Ignore ButtonsHopefully this is fixed in current builds, but in my beta 2 on my contracts that come from a WPF app the stack is very large. The critical Abort/Retry/Ignore buttons are off the bottom of the screen. Also, the red x to close is disabled. I'm really hoping this is already fixed or some part of the following can make the bar to fix:<br/><br/>- Provide a scrolling region to ensure the buttons are always visible at the bottom and the stack is still visible<br/>- Move the buttons to the top and allow us to debug and use a call stack window trace if we need that info<br/>- Enable the Red X to close as Abort or Retry. ANYTHING is better than having a window stuck open on the screen<br/><br/>If you have not seen this and need more information, please let me know.Tue, 17 Nov 2009 17:13:10 Z2009-11-17T17:13:10Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/5cb13a3e-811c-45f9-b7a8-09c39979acd6http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/5cb13a3e-811c-45f9-b7a8-09c39979acd6Richard Blewetthttp://social.msdn.microsoft.com/Profile/en-US/?user=Richard%20BlewettCan't create projects in VS2010 Beta 2 after installing devlabs packageI installed the devlabs package to enable the tool support in VS2010 beta 2 and now cannot create projects<br/><br/>The symptom I see is that it says that from C:\program files\msbuild\4.0\microsoft.common.targets\ImportAfter\codecontracts.targets it cannot find <br/>c:\msbuild\4.0\Microsoft.CodeContracts.Targets <br/><br/><span style="font-size:x-small"> <p>This appears to be a problem with CodeContractsInstallDir not being set up properly - anyone know whats going on?</p> </span><hr class="sig">Richard Blewett, <a href="http://www.thinktecture.com">thinktecture</a> - <a href="http://www.dotnetconsult.co.uk/weblog2">http://www.dotnetconsult.co.uk/weblog2</a><br/> Twitter: <a href="http://twitter.com/richardblewett">richardblewett</a>Thu, 12 Nov 2009 07:52:43 Z2009-11-17T06:35:46Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/91cc63fb-c340-4c4d-9d91-437e65ec0300http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/91cc63fb-c340-4c4d-9d91-437e65ec0300BrendanForsterhttp://social.msdn.microsoft.com/Profile/en-US/?user=BrendanForsterContracts and Constructor OverloadingSo very late to the party, I know. But I've spent a solid day trying to find the limits of the current drop of Code Contracts, and I was wondering if there was a more optimal way to share contracts between constructors than duplicating identical statements:<br/> <br/> For example:<br/> <br/> <div style="color:Black;background-color:White"> <pre><span style="color:Blue"></span> <pre lang="x-c#">public class Person { private string firstName { get; set; } private string lastName { get; set; } private int age { get; set; } public Person(string firstName, string lastName) { Contract.Requires(!string.IsNullOrEmpty(firstName)); // &lt;-- &quot;location related to previous warning&quot; Contract.Requires(!string.IsNullOrEmpty(lastName)); this.firstName = firstName; this.lastName = lastName; } public Person(string firstName, string lastName, int age) // &lt;-- &quot;CodeContracts: requires unproven&quot; :this(firstName, lastName) { // the tools return &quot;requires unproven&quot; warning for this constructor // with an related warning for the marked line above // and suggests adding these Requires // Contract.Requires(!string.IsNullOrEmpty(firstName)); // Contract.Requires(!string.IsNullOrEmpty(lastName)); // uncommenting these lines satisfies the tools Contract.Requires(age &gt; 0); this.age = age; } // ...more code } </pre> <br/></pre> </div> <br/> I'm assuming that the requirements in the first constructor would be applied to the second constructor - is this a limitation of the current tools, or have I overlooked something?<br/>Mon, 16 Nov 2009 06:00:26 Z2009-11-16T21:36:22Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/7f85da54-7f49-47b8-9805-2670d565ba76http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/7f85da54-7f49-47b8-9805-2670d565ba76winSharp93http://social.msdn.microsoft.com/Profile/en-US/?user=winSharp93Problem with Contracts affecting other objectsHello,<br/> <br/> let's take the following class:<br/> <br/> <pre lang="x-c#">public sealed class Foo { public bool IsOpen { get; private set; } public IDisposable Open() { Contract.Ensures(this.IsOpen); this.IsOpen = true; return new Disposer(() =&gt; this.IsOpen = false); } }</pre> <br/> <br/> with that helper class<br/> <br/> <pre lang="x-c#">public sealed class Disposer : IDisposable { private readonly Action _action; private Action Action { get { Contract.Ensures(Contract.Result&lt;Action&gt;() != null); return this._action; } } public Disposer(Action action) { Contract.Requires(action != null); this._action = action; } public void Dispose() { this.Action(); } }</pre> <br/> <br/> Now I am writing code like the following:<br/> <br/> <pre lang="x-c#">Foo foo = new Foo(); using (foo.Open()) { Contract.Assert(foo.IsOpen); } Contract.Assert(!foo.IsOpen);</pre> <br/> <br/> How can I prove the last assert?<br/> <br/> There should be a possibility to modulate Contracts affecting other objects.<br/> <br/> Cheers<br/> winSharp93Sat, 14 Nov 2009 09:16:17 Z2009-11-14T09:16:18Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/b213eb21-4106-42df-90b3-9ecfabadf5a5http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/b213eb21-4106-42df-90b3-9ecfabadf5a5Alexey R.http://social.msdn.microsoft.com/Profile/en-US/?user=Alexey%20R.What is the purpose of ContractPublicPropertyName attribute?Still unclear to me. If there is a public property which correponds to a private field, why can't we just use that public property instead of the private field where we need to satisfy the visibilty rules?Tue, 03 Nov 2009 08:19:22 Z2009-11-13T16:46:51Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/74d9a3c2-e591-430f-a2d7-45dafff2139ahttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/74d9a3c2-e591-430f-a2d7-45dafff2139amiguel katribhttp://social.msdn.microsoft.com/Profile/en-US/?user=miguel%20katribUnexpected BadImageFormatException in a GroupBy poscondition using CodeContract in VS2010 Beta1<span style="LINE-HEIGHT: 115%; FONT-SIZE: 9pt"> <p class="MsoNormal" style="MARGIN: 0in 0in 10pt"><span style="FONT-FAMILY: Calibri; FONT-SIZE: small">The attempt to run a custom GroupBy with the second of the following contract poscondition produces an unexpected bad image format error</span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; COLOR: blue; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas">static</span><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas"> <span style="COLOR: blue">class</span> <span style="COLOR: #2b91af">ContractedQueryMethods</span> {</span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas">&nbsp;</span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas"><span style="mso-spacerun: yes">&nbsp; </span><span style="COLOR: blue">public</span> <span style="COLOR: blue">static</span> <span style="COLOR: #2b91af">IEnumerable</span>&lt;<span style="COLOR: #2b91af">IGrouping</span>&lt;K, T&gt;&gt; GroupBy&lt;T, K&gt;(<span style="COLOR: blue">this</span> <span style="COLOR: #2b91af">IEnumerable</span>&lt;T&gt; source,</span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas"><span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="mso-spacerun: yes">&nbsp;</span><span style="COLOR: #2b91af">Func</span>&lt;T, K&gt; selector) {</span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas">&nbsp;</span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas"><span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="COLOR: green">//This poscondition is correctly verified in runtime</span></span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas"><span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="COLOR: #2b91af">Contract</span>.Ensures(<span style="COLOR: #2b91af">Contract</span>.Result&lt;<span style="COLOR: #2b91af">IEnumerable</span>&lt;<span style="COLOR: #2b91af">IGrouping</span>&lt;K, T&gt;&gt;&gt;().</span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas"><span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>Select(x =&gt; x.Count()).Sum() == source.Count(),</span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas"><span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="mso-spacerun: yes">&nbsp;</span><span style="COLOR: #a31515">"The total items of all groups is equal to the total items in the source"</span>);</span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas"><span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas"><span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="COLOR: green">//The following poscondition produces an unexpected error during injection of the code! </span></span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas"><span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="COLOR: #2b91af">Contract</span>.Ensures(<span style="COLOR: #2b91af">Contract</span>.ForAll&lt;T&gt;(</span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas"><span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>source, </span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas"><span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>(x =&gt; <span style="COLOR: #2b91af">Contract</span>.Result&lt;<span style="COLOR: #2b91af">IEnumerable</span>&lt;<span style="COLOR: #2b91af">IGrouping</span>&lt;K, T&gt;&gt;&gt;().Any(y =&gt; y.Contains(x)))),</span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas"><span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="COLOR: #a31515">"Each item from the source belongs to some group"</span>);</span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas">&nbsp;</span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas"><span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="COLOR: green">//...</span></span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas">&nbsp;</span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas"><span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="COLOR: blue">return</span> <span style="COLOR: #2b91af">Enumerable</span>.GroupBy(source, selector);</span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas"><span style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas"><span style="mso-spacerun: yes">&nbsp; </span>}</span></p> <p class="MsoNormal" style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><span style="FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas"><span style="mso-spacerun: yes">&nbsp; </span><span style="COLOR: green">//...</span></span></p> <p class="MsoNormal" style="MARGIN: 0in 0in 10pt"><span style="LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas">}</span></p> <p class="MsoNormal" style="MARGIN: 0in 0in 10pt"><span style="LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas">Message:</span></p> <p class="MsoNormal" style="MARGIN: 0in 0in 10pt"><span style="LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas">BadImageFormatException was unhandled</span></p> <p class="MsoNormal" style="MARGIN: 0in 0in 10pt"><span style="LINE-HEIGHT: 115%; FONT-FAMILY: Consolas; FONT-SIZE: 9pt; mso-bidi-font-family: Consolas">An attempt was made to load a program with an incorrect format (Exceptipn from HRESULT: 0x80070008</span></p> <p class="MsoNormal" style="MARGIN: 0in 0in 10pt">&nbsp;</p> </span>Fri, 09 Oct 2009 04:52:27 Z2009-11-13T16:42:59Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/711bbf9e-cf40-4343-bc19-2b3a8dbcb489http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/711bbf9e-cf40-4343-bc19-2b3a8dbcb489winSharp93http://social.msdn.microsoft.com/Profile/en-US/?user=winSharp93Contract Information API (Reflection like)?Hello,<br/> <br/> are there any libraries available for getting contract information out of the Contract Assemblies?<br/> <br/> Although it is not that important, it would be to have a &quot;reflection-like&quot; API for this purpose.<br/> <br/> Its usage could look like that schematically:<br/> <pre lang="x-c#">MemberInfo member = //... ContractInformation info = ContractInformation.ForMember(member); foreach (Precondition precondition in info.Preconditions) Console.WriteLine(precondition.Condition);</pre> This would complete the existing reflection APIs.<br/> <br/> Cheers<br/> winSharp93Wed, 11 Nov 2009 20:05:38 Z2009-11-13T16:33:21Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/913080f7-0624-4f42-8a29-4b508e966061http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/913080f7-0624-4f42-8a29-4b508e966061Manuel Fahndrichhttp://social.msdn.microsoft.com/Profile/en-US/?user=Manuel%20FahndrichCodeContracts XML docs not getting generatedThere were reports that the XML contract generation was not working and indeed it ins't in the latest release due to a refactoring bug in the build scripts.<br/><br/>If you want to fix it yourself, here's what to change:<br/><br/>You need to edit both of the following files (need Admin privilege)<br/><br/> <p class=MsoNormal style="margin:0in 0in 0pt"> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"></span></p> <span style="color:#1f497d"><span style="font-size:small"><span style="font-family:Calibri"><span style="">                </span>%Program Files%\Microsoft\Contracts\MsBuild\v3.5\Microsoft.CodeContracts.targets</span></span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"><span style="font-size:small"><span style="font-family:Calibri"><span style="">                </span>%Program Files%\Microsoft\Contracts\MsBuild\v4.0\Microsoft.CodeContracts.targets</span></span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"><span style="font-family:Calibri;font-size:small"> </span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"><span style="font-family:Calibri;font-size:small"> Find the section below and make the Condition match the highlighted text.<br/><br/></span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"><span style="font-size:small"><span style="font-family:Calibri"><span style="">  </span>&lt;!--=====================================================================</span></span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"><span style="font-size:small"><span style="font-family:Calibri"><span style="">        </span>Add post build step for contract XML documentation generation</span></span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"><span style="font-size:small"><span style="font-family:Calibri"><span style="">    </span>======================================================================--&gt;</span></span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"><span style="font-size:small"><span style="font-family:Calibri"><span style="">  </span>&lt;PropertyGroup</span></span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"><span style="font-size:small"><span style="font-family:Calibri"><span style="">      </span><span style="background:yellow">Condition=&quot;'$(CodeContractsEmitXMLDocs)' == 'true'&quot;&gt;</span></span></span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"><span style="font-size:small"><span style="font-family:Calibri"><span style="">    </span>&lt;PrepareForRunDependsOn&gt;$(PrepareForRunDependsOn);ContractXmlDocumentation&lt;/PrepareForRunDependsOn&gt;</span></span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"><span style="font-size:small"><span style="font-family:Calibri"><span style="">  </span>&lt;/PropertyGroup&gt;</span></span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"><span style="font-family:Calibri;font-size:small"> </span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"><span style="font-size:small"><span style="font-family:Calibri">The erroneous version looks like this:</span></span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"><span style="font-family:Calibri;font-size:small"> </span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"><span style="font-size:small"><span style="font-family:Calibri"><span style="">  </span>&lt;!--=====================================================================</span></span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"><span style="font-size:small"><span style="font-family:Calibri"><span style="">        </span>Add post build step for contract XML documentation generation</span></span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"><span style="font-size:small"><span style="font-family:Calibri"><span style="">    </span>======================================================================--&gt;</span></span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"><span style="font-size:small"><span style="font-family:Calibri"><span style="">  </span>&lt;PropertyGroup</span></span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"><span style="font-size:small"><span style="font-family:Calibri"><span style="">      </span><span style="background:yellow">Condition=&quot;'$(CodeContractsEmitXMLDocs)' == 'true' and '$(CodeContractsBuildReferenceAssembly)' == 'true'&quot;&gt;</span></span></span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"><span style="font-size:small"><span style="font-family:Calibri"><span style="">    </span>&lt;PrepareForRunDependsOn&gt;$(PrepareForRunDependsOn);ContractXmlDocumentation&lt;/PrepareForRunDependsOn&gt;</span></span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"><span style="font-size:small"><span style="font-family:Calibri"><span style="">  </span>&lt;/PropertyGroup&gt;</span></span></span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><span style="color:#1f497d"><span style="font-family:Calibri;font-size:small"> </span></span></p><hr class="sig">Cheers, -MaF (Manuel Fahndrich)Thu, 05 Nov 2009 21:55:32 Z2009-11-13T16:25:11Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/459bdcbe-8af2-4781-b1fa-eec1c9d79a52http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/459bdcbe-8af2-4781-b1fa-eec1c9d79a52CommonGenius.comhttp://social.msdn.microsoft.com/Profile/en-US/?user=CommonGenius.comTeam System only?My organization doesn't use Team System, we use Professional Edition. I don't understand why the &quot;academic version&quot; works with any version, but the &quot;commercial version&quot; (which is what we would have to have in order to use Code Contracts in our applications) requires Team System. Is this yet another way to try to force people into buying a more expensive SKU that they don't need?<br><hr class="sig">Moderator | MCTS .NET 2.0 Web Applications | My Blog: http://www.commongenius.comTue, 24 Feb 2009 04:38:34 Z2009-11-13T11:22:13Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/437c060c-aecb-43ff-9b85-3b71861e34eahttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/437c060c-aecb-43ff-9b85-3b71861e34eaLodewijk Voegehttp://social.msdn.microsoft.com/Profile/en-US/?user=Lodewijk%20VoegeMissing invariant, bad(?) invariant, and an odd warninghello,<br/> <br/> I've been playing with CodeContracts, and it's very cool. I'm experimenting with it on a fairly large codebase. some requests and a question:<br/> <br/> please add a Count == Length invariant to BitArray. that's correct per the documentation, and would clean up a heap of warnings for me. Stream.Read()'s postcondition that has already been mentioned before is biting me too (should be offset + count &lt;= buflen instead of &lt;).<br/> <br/> also, ICollection.Add seems to have a &quot;count == oldCount + 1&quot; postcondition on it. the documentation says nothing of this, and for my (pre-4.0) set implementation it isn't true, so this now throws up at runtime. can it be &quot;count == oldCount || count == oldCount + 1&quot; or something?<br/> <br/> and I'm puzzled about a generated warning. consider:<br/> <br/>     class Program { <br/>         static void Foo(List&lt;byte[]&gt; fs) { <br/>             Contract.Requires(fs != null); <br/>             Contract.Requires(fs.Count &gt; 0); <br/>             Contract.Requires(Contract.ForAll(fs, f =&gt; f != null)); <br/>             int l = fs[0].Length; <br/>         } <br/>     }<br/> <br/> this pops an &quot;Possible use of a null array&quot; on the last line, right after the Require() that should tell it none of them are null. am I missing something?<br/> <br/> also, is there a better venue to post these kinds of things than this forum? something like a bugzilla?<br/> <br/> thank you,<br/> LodewijkThu, 12 Nov 2009 20:55:54 Z2009-11-12T20:55:55Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/ad1f8b86-f698-457c-8c50-bd40a3c8cd59http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/ad1f8b86-f698-457c-8c50-bd40a3c8cd59Razispiohttp://social.msdn.microsoft.com/Profile/en-US/?user=RazispioRequires unproven: value != -2147483648The static verifier seems to randomly display this warning that &quot;requires unproven: value != -2147483648&quot;. I encounter this warning three times in my code base, for example here<br/> <pre lang="x-c#">for (int i = 0; i &lt; combo.MultisampleCounts.Count; i++) { int type = combo.MultisampleCounts[i]; Contract.Assume(combo.MultisampleQualities.Count &gt; 0); int quality = combo.MultisampleQualities[0]; if (Math.Abs(type - input.SwapChainDescription.SampleDescription.Count) &lt; Math.Abs(bestCount - input.SwapChainDescription.SampleDescription.Count)) { bestCount = type; bestQuality = Math.Min(quality - 1, input.SwapChainDescription.SampleDescription.Quality); } }</pre> it warns me of the loop initialization (int i = 0) and also of the condition in the if statement.<br/> Do you have any ideas what is going on?Wed, 11 Nov 2009 16:01:33 Z2009-11-13T03:07:45Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/84728d90-b013-49d8-8adc-3bd270abf8abhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/84728d90-b013-49d8-8adc-3bd270abf8abVBA Derkshttp://social.msdn.microsoft.com/Profile/en-US/?user=VBA%20DerksPrecondition failed: offset + count < buffer.Length for Stream.Read methodThe contract libraries shipped with 1.2.21023.14 have the following Contract.Requires for Stream.Read:<br/><br/>   <a title=System.Diagnostics.Contracts.Contract href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib.Contracts::188286aac86319f9/System.Diagnostics.Contracts.Contract">Contract</a>.<a title="void System.Diagnostics.Contracts.Contract.Requires(bool condition, string userSuppliedString, string sourceText);" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib.Contracts::188286aac86319f9/System.Diagnostics.Contracts.Contract/Requires(Boolean,String,String)">Requires</a>((<a title=System.Boolean href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib::b77a5c561934e089/System.Boolean">bool</a>) ((<a title="int offset; // Parameter">offset</a> + <a title="int count; // Parameter">count</a>) &lt; <a title="byte[] buffer; // Parameter">buffer</a>.<a title="int System.Array.Length { ... }" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib.Contracts::188286aac86319f9/System.Array/property:Length:System.Int32">Length</a>), <span style="color:#800000">null</span>, <span style="color:#800000">&quot;offset + count &lt; buffer.Length&quot;</span>);<br/><br/>This contract requires should be:  <a title="int offset; // Parameter">offset</a> + <a title="int count; // Parameter">count</a>) &lt;= <a title="byte[] buffer; // Parameter">buffer</a>.<a title="int System.Array.Length { ... }" href="http://www.aisto.com/roeder/dotnet/Default.aspx?Target=code://mscorlib.Contracts::188286aac86319f9/System.Array/property:Length:System.Int32">Length</a><br/><br/>The same issue applies to other Stream methods.Sat, 07 Nov 2009 21:39:48 Z2009-11-12T14:28:44Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/bf030520-ed9e-4d5f-a3cf-5cd26ceff3b9http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/bf030520-ed9e-4d5f-a3cf-5cd26ceff3b9Nikolai Tillmannhttp://social.msdn.microsoft.com/Profile/en-US/?user=Nikolai%20Tillmanndivision checksWhen I run the static checker on this method, having turned on implicit arithmetic checks, <p><span style="font-size:small"><span style="font-family:Consolas;color:#0000ff;font-size:medium"><span style="font-family:Consolas;color:#0000ff;font-size:medium"><span style="font-family:Consolas;color:#0000ff;font-size:medium">public</span></span></span><span style="font-family:Consolas;font-size:medium"><span style="font-family:Consolas;font-size:medium"> </span></span><span style="font-family:Consolas;color:#0000ff;font-size:medium"><span style="font-family:Consolas;color:#0000ff;font-size:medium"><span style="font-family:Consolas;color:#0000ff;font-size:medium">int</span></span></span><span style="font-family:Consolas;font-size:medium"><span style="font-family:Consolas;font-size:medium"> Divide(</span></span><span style="font-family:Consolas;color:#0000ff;font-size:medium"><span style="font-family:Consolas;color:#0000ff;font-size:medium"><span style="font-family:Consolas;color:#0000ff;font-size:medium">int</span></span></span><span style="font-family:Consolas;font-size:medium"><span style="font-family:Consolas;font-size:medium"> x, </span></span><span style="font-family:Consolas;color:#0000ff;font-size:medium"><span style="font-family:Consolas;color:#0000ff;font-size:medium"><span style="font-family:Consolas;color:#0000ff;font-size:medium">int</span></span></span><span style="font-family:Consolas;font-size:medium"><span style="font-family:Consolas;font-size:medium"> y) { </span></span><span style="font-family:Consolas;color:#0000ff;font-size:medium"><span style="font-family:Consolas;color:#0000ff;font-size:medium"><span style="font-family:Consolas;color:#0000ff;font-size:medium">return</span></span></span></span><span style="font-family:Consolas;font-size:medium"><span style="font-family:Consolas;font-size:medium"><span style="font-size:small"> x / y; }<br/><br/>it does complain about y possibly being null.<br/>But Pex also generates the following test case, that shows that an OverflowException is thrown for (int.MinValue, -1):<br/><br/>[TestMethod]<br/>[PexRaisedException(typeof(OverflowException))]<br/>public void Divide03()<br/>{<br/>    this.Divide(int.MinValue, -1);<br/>}<br/><br/>Why doesn't the static checker complain about this?</span><br/><br/></span></span></p><hr class="sig"> <hr> Nikolai Tillmann - <a href="http://social.msdn.microsoft.com/Forums/en/pex/thread/27c703ea-3928-41ef-b767-638c39966b99">Tell us how you use Pex</a>Thu, 12 Nov 2009 10:38:44 Z2009-11-12T10:38:45Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/37e28a50-bf64-4b02-b384-f55117735690http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/37e28a50-bf64-4b02-b384-f55117735690hwiechershttp://social.msdn.microsoft.com/Profile/en-US/?user=hwiechersContracts and reflectionWhen I verify contracts on this code<br/> <br/> <pre>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; using System.Diagnostics.Contracts; namespace ContractTest { public sealed class MyClass { public int MyProperty { get; set; } public void Test() { Contract.Requires(GetType() != null); Contract.Requires(GetType().GetProperties() != null ); Contract.Requires(GetType().GetProperties().Length == 1); } public void Test2() { Test(); } } } </pre> <br/> I get <br/> <br/> <pre>MyClass.cs(23,13): warning : CodeContracts: requires unproven MyClass.cs(18,13): warning : + location related to previous warning</pre> It's complaining about the line<br/> <pre lang="x-c#">Contract.Requires(GetType().GetProperties().Length == 1); </pre> Am I missing something or is the static checker unable to verify constraints based on reflection?<br/>Wed, 11 Nov 2009 14:22:57 Z2009-11-11T14:22:58Z