Loading user information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading user information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Mike Barnett - Getting started with Code Contracts in Visual Studio 2008

23 minutes, 18 seconds


Right click “Save as…”

Mike Barnett gives a step-by-step tutorial on Code Contracts, a new way to express preconditions, postconditions and invariants in any .NET language. The contracts team have built a runtime instrumentation tool and also advanced static checkers that can understand and reason about contracts.

The Code Contracts library can be downloaded for Visual Studio 2008 from the DevLabs web site. They will be part of the .Net framework 4.0!!!

The Research in Software Engineering team (RiSE) coordinates Microsoft's research in Software Engineering in Redmond, USA.


Follow the discussion

  • Oops, something didn't work.

    Getting subscription
    Subscribe to this conversation
  • Joshua RossJoshRoss Drinking Ovaltine since 2004
    Why put the contract into the method, as opposed to an attribute? How does this code rewriting work with the standard compiler?
  • Sparkywil2300 Super #
    I watched the Spec# Expert to Expert vid with Erik Meijer a while back and it is pretty interesting stuff. Looking forward to this to see how far it has come.
  • My guess is because any expressions (a == b || c != d) you put in an attribute (as a string) can't be verified by the compiler.   Any errors wouldn't be noticed until runtime.
  • There are several reasons to put the contracts into the code as opposed to using custom attributes. hillr's reply is definitely one of the big reasons. You can see our comments on this topic in our FAQ: http://msdn.microsoft.com/en-us/devlabs/dd492004.aspx.

  • I had one question about how to use the contract, and please let me know if this is not the right place to ask.
    Question I have is can the preconditions only go in the constructor?  It makes sense to me that this would be the case, but I figued I'd ask just in case.  I have a custom attribute where I need to check if a variable object inplements a specific interface, but the object is passed in a method and not in the constructor.

  • This is a fine place to ask, but in general we will be monitoring the forum more, so please use that for questions. (http://social.msdn.microsoft.com/Forums/en-US/codecontracts/threads/) But yes, you can have preconditions on any method, not just constructors. In the video, we just ran out of time before we could write a method more complicated than ToInt() and since that one had no parameters, it didn't make any sense to put a precondition on it.

  • Vesuviusvesuvius Count Orlock
    I was really excited about contracts and invariants after watching the Spec# video a while ago. It is an absolute delight to find out that this will become part of the base class library, and I don't need to placate managers or developers to use contracts. Just a call to System.Diagnostics.Contracts will do the trick.

    Thank you for the demonstration, I have put aside all my free time to try this out.
  • Seems that I can soon throw my custom made post condition check snippets away!! Nice
  • Allan LindqvistaL_ Kinect ftw
    woah... that awsome :O
    the static check in perticular is really cool Smiley

    is there a way to customise the runtime checks a little more though? [i just watched the video so this might be obvious in practice]
  • Awesome to say the least!!

    Ensuring that your class is in a correct state would extremely cut back on how often you have to validate your code.

    I'm going to start using this right away.

    Does anyone know if I can use this with Windows Mobile development in Visual Studio?
  • Allan LindqvistaL_ Kinect ftw
    aaaaw its only for vs team suite Sad pleeeeese make a version for those of us not able to convice our managers to get TS? Smiley

    oh wait, there is one for pro as well Big Smile
    (use the second link, not the devlabs one)

Remove this comment

Remove this thread


Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.