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

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

The Discussion

  • User profile image
    Why put the contract into the method, as opposed to an attribute? How does this code rewriting work with the standard compiler?
  • User profile image
    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.
  • User profile image
    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.
  • User profile image

    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: https://msdn.microsoft.com/en-us/devlabs/dd492004.aspx.

  • User profile image
    Don Hansen
    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.

  • User profile image
    This is a fine place to ask, but in general we will be monitoring the forum more, so please use that for questions. (https://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.

  • User profile image
    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.
  • User profile image
    Chris Richner
    Seems that I can soon throw my custom made post condition check snippets away!! Nice
  • User profile image
    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]
  • User profile image
    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?
  • User profile image
    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)

Add Your 2 Cents