Visual Studio 2010 has support for code contracts that allow to express pre-, post-conditions and invariants to your .NET code.

Let’ say you want to create a function to return a random value in a range. This could look like it:

    class Program
    {
        Random rng = new Random();

        public int GetRandom(int min, int max)
        {
            return rng.Next(min, max);
        }

        static void Main(string[] args)
        {
            Program p = new Program();
            int n1 = p.GetRandom(10, 20);
            int n2 = p.GetRandom(10, 10);
        }
    }

However, at a rough analysis one can find two problems:

  • Second call to GetRandom(), is not well formed, because the range is 0
  • Radnom.Next returns a value greater or equal to the first argument, and lower than the second.

What code contracts provide is a mean to check that some statements, like:

  • maximum value of the range should always be greater than the minimum value
  • returned value should always be in the interval, equal or greater than the minimum, and equal or less than then maximum

The first is a pre-requisite, and the second is a post-requisite. We can specify those with:

        public int GetRandom(int min, int max)
        {
            Contract.Requires(max > min);
            Contract.Ensures(Contract.Result() >= min &&
                             Contract.Result() <= max);

            return rng.Next(min, max);
        }

The Contract class is available in namespace System.Diagnostics.Contracts. To enable the static checking, you have to go to Project Properties > Code Contracts and select "Perform Static Contract Checking."

Code Contracts Property Page

Code Contracts Property Page

When you build, you get the following warnings:

Code Contracts warnings

Code Contracts warnings

The first says that the call GetRandom(10, 10) does not match the pre-condition. The second warning indicates that the post-condition is not met. It isn't possible to know whether Random.Next() returns a value that hods the post-condition. But if you check the "Perform Runtime Contract Checking" it asserts at runtime, when the return value is outside the interval (not possible with this code sample).

You can read more about code contracts on the BCL team's blog. It features a list of possible constructs for pre- and post-requisites, but also object invariants.

Code Contracts are also available for Visual Studio 2008. For downloads and additional information check the following links:

, , , , Hits for this post: 24633 .

Chicken chicken chicken chicken chicken chicken chicken chicken. On in other words, this is one of the most funniest presentations I’ve ever seen.

, , Hits for this post: 9345 .

Here is a list of new things in Visual Studio 2010 for unmanaged development.

Visual Studio IDE:

Visual C++

Visual Studio Tools:

Additional readings:

, , , , Hits for this post: 20763 .

Yesterday I have installed Visual Studio 2010 and decided to try my VSBuildStatusAddin. It work on the first try, only with a change to the .AddIn configuration file.

Here is some screen shots in Visual Studio 2010.

VSStatusBuild in Visual Studio 2010

VSStatusBuild in Visual Studio 2010

Visual Studio 2010 about window with VSbuildStatus addin

Visual Studio 2010 about window with VSbuildStatus addin

The images above show a new version of the add-in, 1.1.0. This new version provides the following features:

  • displays the build/clean/deploy progress on a status bar
  • shows information about latest available version with link to download page
  • as already explained, works in Visual Studio 2010

The new version is available at the Visual Studio Gallery.

, , , , Hits for this post: 18684 .

Microsoft has announced the availability of Visual Studio 2010 & .NET 4.0 beta 1 for MSDN subscribers starting this Monday, and for the public starting this Wednesday.

According to Kirill Osenkon, 95% of the planned functionality is already there. This new version of Visual Studio is WPF based. Among the many new features, one important addition is the inclusion of F# as a first class language targeting the .NET framework. More information on the new features are available on Jason Zander’s blog.

The bits can be downloaded from here.

, , Hits for this post: 10137 .

Last week Microsoft published on DevLabs a .NET language for building parallel applications, called Axum, and earlier known as Maestro. This new language is build on the architecture of the web, on the principles of isolation, message-passing, fault-tolerance, loose-coupling. It is said to have a more succinct syntax than Erlang, and have the isolation advantage over MPI, CCR and Asynchronous Agents.

Isolation is key in this architecture and is achieved with:

  • domains, that limits the runtime scope of data to its compile-time scope (objects don’t escape domains)
  • agents, active components that provide access to domains, and live in a thread of their own, different that the callers; their methods are not accessible outside;
  • channels, are the mean to communicate with agents; they are established by the runtime, when agents are created. The most important parts of the channels are the ports (input or output), that can be viewed as queues in which data is placed.

Here are more readings about these topics here:

You can find more about Axum at:

, , , , Hits for this post: 12650 .

After 4 releases of Visual Studio with Help 2 engine, Visual Studio 2010 will bring yet another change: a new help engine, called MS Help 3. The first beta will ship only with online support, but the second beta should contain offline support too.

Changes in the new system include:

  • the new help files have the extension .mshc and are simple zip files containing the HTML files, the images, etc.
  • the new index files have the extesion .mshi; this is proprietary binary file format
  • .mshc and .mshi files are merged in a CAB file, that is passed to the H3 engine for installing; you can also pass just a .mshc file, and the system will create an index file
  • HTML files must be XHTML 1.1 compliant; otherwise the system might not parse them correctly;
  • MS H3 uses meta tags in the <head> section to describe the topic
  • table of contents and visible index are created with meta tags in the HTML files
  • limitation to 500 results for a search is gone
  • links between internal topics use the HTML tag <a href=”">
  • converting from H2 to H3 is possible with some changes as long as the HTML files are XHTML 1.1 compliant

For more information on the new help engine see:

, , , Hits for this post: 15190 .

I have fixed several bugs on version 1.0.4 of my VSBuildStatus add-in for Visual Studio that displays the status of a build/clean/deploy operation. The bugs were mainly about the batch builds. The new version is 1.0.5.

You can download this Visual Studio 2005 & 2008 add-in from here. If you are using a previous build I encourage you download and use the latest.

, , , , Hits for this post: 15619 .