Questions tagged [code-contracts]

Code Contracts is a Microsoft open source project which allows you to express pre-conditions, post-conditions, and assertions directly in code.

Code Contracts is a Microsoft open source project, originating from Microsoft Research, which allows you to express pre-conditions, post-conditions, and assertions directly in code.

A check in a Code Contract is one that, if the code is correct, should always return true. In other words: it should not test whether the database is accessible or whether the user has entered an empty string -- those are outside factors. Code Contracts are there only to make sure your code is correct and maintains its invariants.

So, typically, you would do input parameter validation on public methods by explicitly checking the value and then throwing an exception if out of range. But for private methods -- which get called only by other code, and so can expect certain pre-conditions -- the input parameters can be checked with Code Contracts.

private void MyMethod(string machineId, int age)
{
    Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(machineId));
    Contract.Requires<ArgumentOutOfRangeException>(machineId.StartsWith("PEK"));
    Contract.Requires<ArgumentOutOfRangeException>(age > 16);
    //...
}

Post-conditions are expressions that must evaluate to true when the method is finished. A typical example is to verify that the output of a function is never null, or that it is within a certain range.

private string MachineNameOf(int id)
{
    Contract.Requires<ArgumentOutOfRangeException>(id > 100);
    Contract.Ensures(Contract.Result<string>() != null);
    //...
}

GitHub: https://github.com/Microsoft/CodeContracts

Tools at: http://visualstudiogallery.msdn.microsoft.com/1ec7db13-3363-46c9-851f-1ce455f66970.

See also (historical): http://research.microsoft.com/en-us/projects/contracts.

649 questions
7
votes
5 answers

How Do You Configure Pex to Respect Code Contracts?

Given the following example code, how can I configure Pex to respect my Code Contracts? public static IEnumerable Administrators(this UserGroup userGroup) { Contract.Requires(userGroup != null); …
Jim G.
  • 15,141
  • 22
  • 103
  • 166
7
votes
2 answers

how to config Code Contracts in Interface

I can't configurate Code Contracts in my class. I've followed the documentation and the example but it doesn't work. I would to insert Code Contracts condition into my interface, here my code The interface …
Faber
  • 2,194
  • 2
  • 27
  • 36
7
votes
2 answers

Code Contracts trying to get build errors instead of warnings

I'm trying to get VS2010 Ultimate with Code Contracts to generate Errors instead of Warnings. I have this simple test program: using System.Diagnostics.Contracts; namespace MyError { public class Program { static void Main(string[] args) …
larry
  • 71
  • 1
7
votes
2 answers

How to use code contracts when deriving from interfaces like IDictionary?

One class I am writing implements IDictionary. In my CopyTo implementation, I would like to use code contracts: stuff like Contract.Requires(array != null). But, I get this warning (with some namespaces removed…
Domenic
  • 110,262
  • 41
  • 219
  • 271
7
votes
2 answers

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…
tmont
  • 2,562
  • 20
  • 15
7
votes
1 answer

Code contract rewrite failing with 'Could not resolve member reference'

Note that this might be a duplicate of this question, I'm not entirely sure. My problem is that I have a class library project which has a reference to a third-party type library (COM). I want to put contracts into the methods in the class library,…
WalderFrey
  • 575
  • 2
  • 17
7
votes
1 answer

How does Code Contracts know that ToString overrides shouldn't return null?

I'm using Microsoft's Code Contracts extension with C#. When I write a class with an overridden ToString implementation that returns null, it correctly identifies the issue: I assumed this was because Microsoft uses Code Contracts internally, and…
Will
  • 2,086
  • 23
  • 30
7
votes
2 answers

Code contracts for .NET 3.5 messes up VS10's debugger

I've recently migrated a lot of manual precondition testing and exception throwing with code contracts. Instead of upgrading to .NET 4, I've been using the Microsoft.Contracts.dll assembly so I could stick to .NET 3.5 a bit longer (this is a library…
Trillian
  • 6,207
  • 1
  • 26
  • 36
7
votes
6 answers

F# and statically checked union cases

Soon me and my brother-in-arms Joel will release version 0.9 of Wing Beats. It's an internal DSL written in F#. With it you can generate XHTML. One of the sources of inspiration have been the XHTML.M module of the Ocsigen framework. I'm not used to…
Johan Jonasson
  • 500
  • 2
  • 18
7
votes
5 answers

CodeContracts: Possibly calling a method on a null reference

I'm having an argument with the CodeContracts static analysis tool. My code: (ASCII version) The tool tells me that instance.bar may be a null reference. I believe the opposite. Who is right? How can I prove it wrong?
dtb
  • 213,145
  • 36
  • 401
  • 431
7
votes
0 answers

Code Contracts can't connect to the cache it itself created

On a specific project that uses CC, the following message is displayed in visual-studio-2013's Error List window upon build: CodeContracts: Cannot connect to the cache. The CodeContracs static check will not run Googling around, I've found out…
Crono
  • 10,211
  • 6
  • 43
  • 75
7
votes
2 answers

VS2013 with ReSharper 8.2 not recognizing Code Contracts?

I have the following software: Visual Studio 2013 Update 2 Code Contracts (1.6.60617.15) ReSharper C# edition (8.2.0.2160) ReSharper Code Contracts (1.0.0.0) When I open a method with code contracts, ReSharper is confused about the contract: It…
Deathspike
  • 8,582
  • 6
  • 44
  • 82
7
votes
1 answer

Code Contracts: How do I state in a post-condition that a field/property's value has not changed?

I'll best just show with a code example what I would like to accomplish? class SomeClass { public int SomeProperty; public void SomeOperation() { Contract.Ensures( "SomeProperty's value has not changed." ); …
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
7
votes
1 answer

How do I use Code Contracts to have a compile time assert in C#?

According to this answer C# now has "code contracts" that should be usable instead of C++ compile time asserts. Now I have this magic code: IntPtr pointer; //blahblahblah pointer = new IntPtr(pointer.ToInt32() +…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
7
votes
2 answers

Code Contracts - should private methods be checked for pre and post conditions?

I am trying to get up to speed on Code Contracts. I like the concept, but in practice, I don't see the value of adding Contract.Requires on numerous private methods, some of which are only a line or two long. I can see the point on Public methods,…
Greg Gum
  • 33,478
  • 39
  • 162
  • 233