.NET 4.0 code contracts – How will they affect unit testing?

I don’t think unit testing and contracts interfere with each other that much, and if anything contracts should help unit testing since it removes the need to add tedious repetitive tests for invalid arguments. Contracts specify the minimum you can expect from the function, whereas unit tests attempt to validate the actual behaviour for a … Read more

How mature is the Microsoft Code Contracts framework?

The last mature response to this was in 2009, and .NET 4 is out. I figure we’re due for an update: Code Contracts might well be mature enough for your Debug releases. I realise this is somewhat of an upgrade from “Harmless” to “Mostly Harmless”. The Code Contracts home page links to quite thorough documentation … Read more

Debug.Assert vs Code Contract usage

These are different things. A debug assert is only executed when the code is compiled as debug and therefore will only check/assert under debug. The idea is to use this for “sanity checks” for code you are developing. Code contracts can be used in either debug or release. They assure that pre and post conditions … Read more

Does Visual Studio 2017 work with Code Contracts?

As others have noted, Microsoft hasn’t prioritized Code Contracts and its long-term support remains unclear (though there has been some ongoing discussion about language-level integration via Roslyn). As of March 11th, 2017, however, community contributor Yaakov has, at least, updated the source code to include the Visual Studio 2017 build targets (thank you!). This version … Read more

ReSharper – Possible Null Assignment when using Microsoft.Contracts

Note: as of the current R# 8.0 EAP, this functionality is included. Here’s the solution for the current (i.e. .NET 4.0) version of Code Contracts: Inside …\ExternalAnnotations\mscorlib\Contracts.xml, add the following: <assembly name=”mscorlib”> <member name=”M:System.Diagnostics.Contracts.Contract.Assert(System.Boolean)”> <attribute ctor=”M:JetBrains.Annotations.AssertionMethodAttribute.#ctor”/> <parameter name=”condition”> <attribute ctor=”M:JetBrains.Annotations.AssertionConditionAttribute.#ctor(JetBrains.Annotations.AssertionConditionType)”> <argument>0</argument> </attribute> </parameter> </member> <member name=”M:System.Diagnostics.Contracts.Contract.Assert(System.Boolean, System.String)”> <attribute ctor=”M:JetBrains.Annotations.AssertionMethodAttribute.#ctor”/> <parameter name=”condition”> <attribute ctor=”M:JetBrains.Annotations.AssertionConditionAttribute.#ctor(JetBrains.Annotations.AssertionConditionType)”> <argument>0</argument> </attribute> … Read more

Really trying to like CodeContracts in C#

This feels like such a cludge to me, I wish there was a cleaner way to document requirements, either via attributes or some form of built in language support. The CC team have stated that using Attributes just isn’t powerful enough, because you can’t include things like lambdas in them. They could include things like … Read more