microsoft-contracts

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 ...

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 >...

Design by Contract: Can you have an Interface with a Protocol?

Hi all, I'm pretty new to the concept of Design by Contract, but so far, I'm loving how easy it makes it to find potential bugs. However, I've been working with the Microsoft.Contracts library (which is pretty great,) and I have run into a road block. Take this simplified example of what I'm trying to do: public enum State { NotReady...

Can Microsoft.Contracts' static checker be used without Team System?

Aside from the requirement on Visual Studio Team System to be able to install Microsoft.Contacts with the static checker, is it possible to run the static checker without team system? Or, does it depend on an API exposed by studio's team system components? Also, is it within the license to copy the static checker from a computer with ...

What does it take to prove this Contract.Requires?

I have an application that runs through the rounds in a tournament, and I am getting a contract warning on this simplified code structure: public static void LoadState(IList<Object> stuff) { for(int i = 0; i < stuff.Count; i++) { // Contract.Assert(i < stuff.Count); // Contract.Assume(i < ...

.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...

.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...

How do I set up Microsoft Contracts static checking in Visual Studio 2010?

I recently downloaded Visual Studio 2010b2, and wanted to re-evaluate some of my questions about the Microsoft contracts static checker. I managed to re-use most of the code by using the System.Diagnostics.Contracts namespace for the code, but I am unsure of how to enable the static checker. Do I need an additional plug-in? I was unde...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;? ...

Why is this C# Code Contract malformed?

Visual Studio shows an error when I write this contract below. Error 20 Malformed contract section in method '....get_Page' Is the problem with the 'if' block? public int? Page { get { int? result = Contract.Result<int?>(); if (result != null) Contract.Ensures(result >= 0); return default(int?); } } EDIT: Lass...

C#: Code Contracts vs. normal parameter validation

Hi, consider the following two pieces of code: public static Time Parse(string value) { string regXExpres = "^([0-9]|[0-1][0-9]|2[0-3]):([0-9]|[0-5][0-9])$|^24:(0|00)$"; Contract.Requires(value != null); Contract.Requires(new Regex(regXExpres).IsMatch(value)); string[] tokens = value.S...