code-contracts

Books on Code Contracts in C# 4.0

Altough I've known Code Contracts for some time, as I've used it a bit in Java, I would like to start using them in C#, now that they are part of C# 4.0. I am looking on learning material, books or vids. Altough tutorials are also welcome, I'd like to have something comprehensive to read, not the typical "here's how you use Requires and...

Method that does conditional return of method calling it?

Ok this might be a bit of hack but bear with me :) The background is that i'm tired of methods that that some if-statement to that messes up indention for the whole method, like: public SomeClass DoStuff(string inputStr) { SomeClass result =null; if (IsOpenFilter(inputStr)) { .... } return result; } So I was thinking ,...

Are you using CodeContracts? Do you knowabout any decent-sized project out there which uses is?

Are you using CodeContracts? Do you know about any decent-sized project out there which uses is? ...

Can I use .NET 4 Code Contracts and remain compatible with .NET 3.5?

.NET 4 introduced Code Contracts as a new feature. I'd like to use CC, but provide files that can still run in 3.5 SP1. Is that possible? Can I only use parts of the new functionality? Apparently it is possible to have CC only do static checks without being included in the binary files, is that correct? I'm aware CC was available for 3....

How good idea is it to use code contracts in Visual Studio 2010 Professional (ie. no static checking) for class libraries?

I create class libraries, some which are used by others around the world, and now that I'm starting to use Visual Studio 2010 I'm wondering how good idea it is for me to switch to using code contracts, instead of regular old-style if-statements. ie. instead of this: if (fileName == null) throw new ArgumentNullException("fileName");...

How come you cannot catch Code Contract exceptions?

System.Diagnostics.Contracts.ContractException is not accessiable in my test project. Note this code is purely myself messing around with my shiney new copy of Visual Studio, but I'd like to know what I'm doing wrong. I'm using the professional edition of VS, therefore I do not have static checking. In order to still use code contracts ...

IOC Container Handling State Params in Non-Default Constructor

For the purpose of this discussion, there are two kinds of parameters an object constructor might take: state dependency or service dependency. Supplying a service dependency with an IOC container is easy: DI takes over. But in contrast, state dependencies are usually only known to the client. That is, the object requestor. It turns ...

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

Code Contracts Vs. Object Initializers (.net 4.0)

At face value, it would seem that object initializers present a problem for .net 4.0 "code contracts", where normally the invariant should be established by the time the object constructor is finished. Presumably, however, object-initializers require properties to be set after construction is complete. My question is if the invariants ...

How do I imply code contracts of chained methods to avoid superfluous checks while chaining?

I'm using Code Contracts in C# 4.0. I'm applying the usual static method chaining to simulate optional parameters (I know C# 4.0 supports optional parameters but I really don't want to use them). The thing is that my contract requirements are executed twice (or possibly the number of chained overloads I'd implement) if I call the Init(s...

Design by contracts and constructors

I am implementing my own ArrayList for school purposes, but to spice up things a bit I'm trying to use C# 4.0 Code Contracts. All was fine until I needed to add Contracts to the constructors. Should I add Contract.Ensures() in the empty parameter constructor? public ArrayList(int capacity) { Contract.Requires(capacity > 0);...

For reliable code, NModel, Spec Explorer, F# or other?

I've got a business app in C#, with unit tests. Can I increase the reliability and cut down on my testing time and expense by using NModel or Spec Explorer? Alternately, if I were to rewrite it in F# (or even Haskell), what kinds (if any) of reliability increase might I see? Code Contracts? ASML? I realize this is subjective, and p...

What to do when using Contract.Assert(true) and the method must return something?

I have a bit of code with the following logic: //pseudo-code foreach (element in elementList) { if (element is whatever) return element; } } In theory, there is always one element that is whatever, so this method should pose no problems. In any case, I've put an assertion on the end of the method just to be sure: //ps...

Awkward looking uses of Contract.ValueAtReturn()

I am designing a method that will add an element to an internal list. The structure of the class is something along the lines of: class MyCustomerDatabase { private IList<Customer> _customers = new List<Customer>(); public int NumberOfCustomers { get { return _customers; } } public void AddCustomer(Customer customer) {...

How does Contract.Exists add value?

I am just starting to learn about the code contracts library that comes standard with VS2010. One thing I am running into right away is what some of the contract clauses really mean. For example, how are these two statements different? Contract.Requires(!mycollection.Any(a => a.ID == newID)); Contract.Requires(!Contract.Exists(mycollec...

Question about [Pure] methods

Is the following method Pure? I'd say so, as it doesn't change in anyway the current class, thus, everything we can now currenly "see" in the class, before running this method will still be exactly the same after. Am I correct? class Set { ... public ISet<T> UnionWith(ISet<T> set) { ISet<T> unionSet = ... foreach...

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 the OCaml syntax, but I do understand XHTML.M somehow statically check if attributes and c...

Which 3rd party Code-by-Contract library is most like MS's .NET 4.0 library?

I want to jump into coding by contract. I got VS2010 (with the C# 4.0 compiler) but I have to target the 3.5 framework. What 3rd party code by contract library has classes and interface the most like the .NET 4.0 ones? ...

What does "Contract can't be in try block" mean?

I'm using the 3.5 library for microsoft code contracts public object RetrieveById(int Id) { //stuff happens... Contract.Ensures(newObject != null, "object must not be null"); return newProject; //No error message if I move the Contract.Ensures to here //But it isn't asserting/throwing a contract exception here ei...

So do C#4.0 Code Contracts Actually Do Anything?

After reading about the System.Diagnostics.Contracts.Contract static class that has been influenced by the awesomeness of Spec# I was thrilled and immediately started peppering my code with calls to Contract.Requires() and Contract.Ensures(). I guess it's just because my code is so super-awesome and bug-free that checking that those c...