code-contracts

Code Contracts and Auto Generated Files

When I enabled code contracts on my WPF control project I ran into a problem with an auto generated file which was created at compile time (XamlNamespace.GeneratedInternalTypeHelper). Note, the generated file is called GeneratedInternalTypeHelper.g.cs and is not the same as the GeneratedInternalTypeHelper.g.i.cs which there are several o...

Usefulness of System.Diagnostics.Contracts in question

I've been playing with the new System.Diagnostics.Contracts class because it seemed very useful at first. Static methods to check inbound arguments, return values, etc. It was a clean interface and could replace lots of if-then statements and internally built library tools. However, it seems less than useful in most runtime situations. ...

Is there a way not to let MsBuild run static contract analysis with code contracts?

In my project, static checking is disabled, but still, when I run msbuild.exe with cmd, it starts static checking for each project... Is there a way, with parameters, to disable this? ...

Am I implementing this simple contract incorrectly?

This is my code: public class RegularPolygon { public int VertexCount; public double SideLength; public RegularPolygon(int vertexCount, double sideLength) { Contract.Requires(vertexCount >= 3); VertexCount = vertexCount; SideLength = sideLength; } [ContractInvariantMethod] private vo...

Naming conventions for Code Contract classes of Interface types

Hi All, I'm using the Code Contracts classes in the System.Diagnostics.Contracts namespace to define some contracts for my objects and I'm wondering how others name their contract classes when the contract is defined against a base interface. Let me illustrate with a small example: [ContractClass(typeof(AnimalContract))] public interfa...

Code contracts slow down Visual Studio 2008

Since I started to use Code Contracts in Visual Studio 2008 (C#), building my dlls goes really slow. I haven't got static checking turned on, but apparently rewriting takes forever... Is there way to solve this and will this be improved in future versions of Microsoft Code Contracts? Update: When building in VS2008, even when Static an...

Have to prove base class property invariant in all derived classes?

I have a base class in which I'm trying to use the Null Object pattern to provide a default logger implementation which can then be changed by IoC setter injection at a later stage. public interface ILog { void Log(string message); } public class NoOpLogger: ILog { public void Log(string message) { } } public abstract cla...

CodeContracts - false positives

I've just started experimenting with CodeContracts in .NET 4 on an existing medium-sized project, and I'm surprised that the static checker is giving me compile-time warnings about the following piece of code: public class Foo { private readonly List<string> strs = new List<string>(); public void DoSomething() { // Comp...

system.diagnostics.contracts.contract exists in both v4.0 v3.5

I just upgraded to visual studio 2010 and installed the code contracts msi. When I try to use it I get a compiler error that the system.diagnostics.contracts.contract dll exists in both the v3.5 and v4 framework. Anyone know what I should do to resolve this? Thanks. ...

C# Code Contracts: Why can't this simple condition be proven?

I'm doing a simple test of Code Contracts. The following code is in a winform. This passes (of course): private void Test(Form form) { Contract.Requires(!string.IsNullOrEmpty(form.Name)); MessageBox.Show(form.Name); } protected override void OnLoad(EventArgs e) { if (!string.IsNullOrEmpty(Na...

Is this a bug in the Code Contracts rewriter?

I am experimenting with .NET Code Contracts. The following code runs just fine when runtime contract checking is turned off, but fails when runtime contract checking is turned on: using System.Collections.Generic; using System.Diagnostics.Contracts; namespace ConsoleApplication1 { public class Item<T> where T : class { } public...

Using Code Contracts in library code built with MSBuild

I've started using Code Contracts in all new code I'm writing, such as in a framework library I'm building to help bootstrap IoC, O/RM, etc., in an ASP.NET MVC application. I've written a simple build script for this framework library that looks like the following: @echo off echo. echo Cleaning build output (removing 'obj' and 'bin' fol...

Microsoft Code Contracts and CI build server.

Hello all. We are migrating to .NET 4 and very interested in implementing new Design By Contract capabilities. As we know Code Contract engine requires installation of Code Contract addin and VS Ultimate or Premium (for static checking). Here is my questions: Can I use code contract rewriting without installing VS on CI build ...

Does Sandcastle support code contracts?

My library uses code contracts. Is there a way to incorporate these contracts in the documentation generated by sandcastle? ...

Why contract is malformed when using default(Type)?

Hi, When compiling code which uses code contracts, I have a very strange error I don't understand. [ContractInvariantMethod] private void ObjectInvariant() { Contract.Invariant( this.isSubsidiary || this.parentCompanyId == default(Guid)); } fails with the following error: Malformed contract. Found Invariant af...

Are Microsoft Code Contracts unsuitable for validating user input?

I've seen it written elsewhere on SO that while the Enterprise Library Validation Application Block is geared towards validating user inputs, Code Contracts are meant to prevent programmer errors. Would you support this opinion? Why? ...

Contract class should be an abstract class

The following code gives me the warning Contract class 'FooContracts' should be an abstract class. From all the examples I've read online (e.g. http://www.infoq.com/articles/code-contracts-csharp), this should work (presumably without compiler warnings). [ContractClass(typeof(FooContracts))] public interface IFoo { void Bar(string foo...

Code contracts. How suppress warnings on generated code?

How can I turn off static checks on my Linq2Sql code? ...

Eiffel: loosening the pre-conditions and tightening the post-conditions?

In Eiffel it is said that we should "loosen the pre-conditions and tightening the post-conditions", but I am not sure what this means. How does this benefit/is benefited by sub-classing? Thank you ...

Static verification limitations when using pure functions in C# code contracts?

I'm trying to statically verify the following partial implementation of an array-based stack with code contracts. The method Pop() uses the pure function IsNotEmpty() to ensure that the subsequent array access will be at/above the lower bound. The static verifier fails and suggests that I add the precondition Contract.Requires(0 <= this....