code-contracts

Contract based Programming

Can someone explain the concepts that Spec# might be moving into C# 4.0, regarding Code Contracts? What are code contracts (Looks to be a compile time checking pattern) should I be excited about this? Am I correct in assuming that we move what would be runtime checks to compile time? Thanks! ...

How do I indicate that a method never returns a null using code contracts?

How do I indicate that a method never returns a null? Currently this is my code. Line 19 gets an Ensures not proven message, even though CreateFunction assumes that the result is not nothing. 1 <Pure()> Public Function CreateFunction(Of TArg1, TArg2, TResult)(ByVal body As Func(Of Expression, Expression, BinaryExpression)) As F...

How mature is the Microsoft Code Contracts framework?

Microsoft has recently put a release of their Code Contracts framework on DevLabs with a commercial license. We're interested on using them in our project (mostly C#, some C++/CLI) to gradually replace all the custom validation code, but I'm keen to know about the experience other people have had with it before we commit to it, specifica...

What tooling do you use to do Design by Contract?

I used to use Microsoft CodeContracts for three weeks and now half of my code is just contracts. I have dozens of unproved places, I cannot use runtime-check because IL rewrite prevents coverage tool to show something and compile time is less then acceptable. I do not like this. And seems now is a good time to ask a help. What tooling ...

Projects that make use of Spec#/Code Contracts.NET

Hi, I am interested in finding out more about the use of Spec# and/or its spin-off project Code Contracts. Is there a live project that currently makes use of either technology? Thanks, MagicAndi. EDIT: Given David's answer below, I have opened the question up to include any project, not simply open source. ...

How will using Code Contracts in my web project affect deployment?

If code contracts are used in an application then do we need to install anything (from Code Contracts package) on production server like putting assemblies into GAC or running code contracts installation package on production server? Or just putting libraries in bin folder will work? ...

ReSharper - Possible Null Assignment when using Microsoft.Contracts

Is there any way to indicate to ReSharper that a null reference won't occur because of Design-by-Contract Requires checking? For example, the following code will raise the warning (Possible 'null' assignment to entity marked with 'NotNull' attribute) in ReSharper on lines 7 and 8: private Dictionary<string, string> _Lookup = new Dictio...

Microsoft Contracts: Assembly load resulted in metadata import warning

I'm trying to learn my way around the Microsoft Code Contracts libraries, and I have the following simple function: internal static Engine CreateBuildEngine(Microsoft.Build.Framework.ILogger logger) { Contract.Requires( logger != null ); var engine = new Engine { DefaultToolsVersion = "3.5" }; engine.RegisterLogger(logger);...

Bug in iterators with code contracts?

The following code fails on the pre condition. Is this a bug in code contracts? static class Program { static void Main() { foreach (var s in Test(3)) { Console.WriteLine(s); } } static IEnumerable<int>Test (int i) { Contract.Requires(i > 0); for (int j = 0; j < i;...

Can Microsoft Code Contracts be used with an ASP.NET Website?

I'm currently using Microsoft Code Contracts in an ASP.NET MVC application without any issues but I can not seem to get it quite running in a basic ASP.NET Web site. I'm not entirely sure it was made to work with this type of project (although it shouldn't matter) so I wanted to bring it up to everyone. I can compile the contracts just ...

CodeContracts: How to fullfill Require in Ctor using this() call?

I'm playing around with Microsoft's CodeContracts and encountered a problem I was unable to solve. I've got a class with two constructors: public Foo (public float f) { Contracts.Require(f > 0); } public Foo (int i) : this ((float)i) {} The example is simplified. I don't know how to check the second constructor's f for being >...

Why is ccrewrite.exe not doing anything from the command line?

I've got Code Contracts working fine from inside Visual Studio 2010, but I can't get ccrewrite.exe to do anything useful from the command line. Here's a sample app: using System.Diagnostics.Contracts; public class Dummy { public static void Main(string[] args) { Contract.Requires(args.Length > 0); } } I then compi...

How free can I be in the code in an object invariant?

I'm trying to demonstrate invariants in Code Contracts, and I thought I'd give an example of a sorted list of strings. It maintains an array internally, with spare space for additions etc - just like List<T>, basically. When it needs to add an item, it inserts it into the array, etc. I figured I had three invariants: The count must be ...

Should the Code Contracts static checker be able to check arithmetic bound?

(Also posted on the MSDN forum - but that doesn't get much traffic, as far as I can see.) I've been trying to provide an example of Assert and Assume. Here's the code I've got: public static int RollDice(Random rng) { Contract.Ensures(Contract.Result<int>() >= 2 && Contract.Result<int>() <= 12); if (rng ==...

.NET code contracts: can it get more basic than this?

I was just messing around to answer someone's question here on Stack Overflow, when I noticed a static verification warning from inside my Visual Studio (2008): string[] source = { "1", "A", "B" }; var sourceObjects = Array.ConvertAll(source, c => new Source(c)).ToArray(); I'm getting the message requires unproven source != null. It s...

What binary rewriter is used to implement Microsoft's Code Contracts?

I am talking about those Code Contracts that will end up in .NET 4.0. What binary rewriter do they use to inject the code that makes it all work and is it publicly available? I hope it's not just a compiler thing because I would love to be able to use their binary rewriter for AOP. ...

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

For example this article introduces them. What is the benefit? Static analysis seems cool but at the same time it would prevent the ability to pass null as a parameter in unit test. (if you followed the example in the article that is) While on the topic of unit testing - given how things are now surely there is no point for code cont...

Exclude System.Diagnostics.Contracts When Using PartCover

I am trying out the .net Code Contracts fro .net 3.5 I have some unit test that I am running PartCover over to calculate the code coverage. PartCover keeps including the System.Diagnostics.Contracts in my report. Here are the rules I am using to include MyProject and exclude everything else. <Rule>+[MyProject.DomainModel]*</Rule> <...

Is there any way to make Code Contracts work with LINQ?

Code Contracts keep giving me "Possibly calling a method on a null reference" warnings for all of my LINQ statements, and I can't find a way to silence them. For example, the following method generates two such warnings because I'm accessing the "Make" and "Model" properties of the "car" object without first checking for null. publ...

Code Contracts in .NET 4.0, no joy for non-nullable reference types fans?

I've been playing with Code Contracts on VS2008 (http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx). They surely are nice and provide a solid alternative to if-then-throw checks inside methods. Nonetheless I've been hoping that they could satisfy the need that I strongly feel for non-nullable reference types. Alas, from what I could...