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
0
votes
1 answer

What could be causing Code Contracts to cancel a build operation?

I'm encountering a very strange issue when trying to build a solution that uses Code Contracts. For no appearant reason, at some point build gets canceled - or so it says in VS's output window. There are no compilation error of any sort and if I…
Crono
  • 10,211
  • 6
  • 43
  • 75
0
votes
1 answer

Temporarily disable code contracts inside a block

Is it possible to disable run time (and compile time) checks on a class using a method call or similar? I am having trouble with my classes with invariants and using them with external libraries that construct instances dynamically. I would like to…
David Miani
  • 14,518
  • 2
  • 47
  • 66
0
votes
1 answer

How can I make Code Contracts play nicely with a WiX Custom Action project?

I have run into a problem in a solution containing a WiX manged custom action project, which is also using MS RiSE Code Contracts. The problem is that the code contracts tools fail with the following error: I found a few mutterings about this issue…
Tim Long
  • 13,508
  • 19
  • 79
  • 147
0
votes
1 answer

What happens if Code Contracts isn't installed?

My csproj defines a build configuration 'Debug-plus' with Code Contracts. TRACE;DEBUG;CONTRACTS_FULL
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
0
votes
0 answers

Code contract is not showing errors

I found a strange behavior in contract code tool in VS. The image above shows no error after building the project: but when I add some code on, it shows the error: What am I missing?
MuriloKunze
  • 15,195
  • 17
  • 54
  • 82
0
votes
1 answer

Code Contracts in C# 4.0+ / Visual Studio 2013

I'm pretty sure I tried to use Code Contracts in C# code by just including a reference to System.Diagnostics.Contracts Namespace http://goo.gl/wZIg54 . But Code Contracts for .NET page suggests I need to download MSI package. Why is it so? Are Code…
vkelman
  • 1,501
  • 1
  • 15
  • 25
0
votes
2 answers

How to ensure that implementations of an interface have a connection string if they are backed by a database using code contracts

Imagine you've an interface like this: public interface IPersonManager { public void AddPerson(string name); } ...and an implementation which we'll going to call DefaultPersonManager. Let's say we want to be sure that any implementation of…
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
0
votes
1 answer

Code Contracts Static Checking

I have a snippet of code using code contracts that I expect to fail at compile time ... int myInt = -1; Contract.Ensures(myInt > 0); ... The line using the contract is grayed out and I get a tool tip saying, "Method invocation is skipped. Compiler…
Teeknow
  • 1,015
  • 2
  • 17
  • 39
0
votes
1 answer

Compiler-Aided nullity contracts in c#?

In Java it is possible to annotate methods with annotations that allow some processor to infer nullity and give you a warning/compiler error if you'd violate the nullity contract: @NotNull public String returnSomething() { return null; //…
Jan Thomä
  • 13,296
  • 6
  • 55
  • 83
0
votes
1 answer

Is that possible to add CodeContracts library to Visual Studio 2005?

I was just wondering, would I be able to add and use CodeContracts under System.Diagnostics in Visual Studion 2005? Thank's
eugene.it
  • 417
  • 3
  • 13
  • 32
0
votes
1 answer

Is it a bad idea to rely on contracts only for exported interfaces used as dependencies?

First, I love Code Contracts but I'm a little confused with one thought: My class MyClass uses a dependency being an interface IDependency for which contract class (internal abstract class IDependencyContracts) is declared and my class heavily…
Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137
0
votes
1 answer

Code Contracts and Code Coverage

I'm using Code Contracts in my C# application, together with unit tests. When I ask for the code coverage results of the unit tests, lines containing code contracts are reported as "not covered". Lets take for example a method that has 2…
drl
  • 811
  • 2
  • 11
  • 21
0
votes
1 answer

Why doesn't ccrewrite get called when I publish my project(s)?

I finally have ccrewrite working, and my unit tests are passing, but when I publish a web project, it keeps blowing up as if it hadn't been rewritten. After checking with JustDecompile, the code hasn't been rewritten: …
0
votes
2 answers

Ensure interface readonly property does not change

Consider the following interface: interface IToto { string Name {get;} } Using Code Contracts, how can I make sure that the Name property value never changes? I tried using Contract.OldValue within a ContractInvariantMethod-marked method but…
Crono
  • 10,211
  • 6
  • 43
  • 75
0
votes
1 answer

CodeContract error CC1025 on private execute method

I have a private execute method ClickExecute for a Command object Command. I am binding a parameter object which is of type TypeA. It has a property IsValid which I want to check in code contract as shown below. ClickCommand = new…
Carbine
  • 7,849
  • 4
  • 30
  • 54