Geeks With Blogs
Darren Fieldhouse

One of the many new features Microsoft is planning to bring to .NET 4.0 is Code Contracts. In a nutshell, this is a feature that allow a developer to formally specify the requirements of a function, and the state of the system when the function completes (e.g. A parameter will be non-null, or a property of the object will be greater than 0 at the end of the function). The initial work for this came from the Spec# project, a Microsoft Research project that extended the C# language to include keywords for specifying the contracts, but it looks like .NET 4.0 will include the features in the form of framework methods, for example:

void DoSomething(int parameter)
  CodeContract.Requires(parameter >= 0);

I think this will be a great feature, but it seems to me that it would be better expressed as metadata using attributes rather than in the function body itself. I guess we will just have to wait and see what the community reaction is to this feature, and how it ends up being implemented in the release version!

I'm also interested to see how this will work with Dynamic Data, an ASP.NET feature that already uses metadata on LINQ classes to specify constrains which are used to generate validators in the UI. This seems like a perfect way to specify those constrains.

Posted on Tuesday, November 25, 2008 11:35 AM | Back to top

Comments on this post: Code Contracts in .NET 4.0

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © DarrenFieldhouse | Powered by: