Code Contracts: Contract.OldValue<T>() (8 of n)

Posted on Tuesday, July 20, 2010 10:47 AM

Continuing our series on Code Contracts. This time with the Contract.OldValue<T>() method. This method will return the value of the given variable at the start of the enclosing method or property. It is however only valid to use this method in a post conditional Contract.Ensures method call.

And that immediately indicates it’s purpose. It should be used to enable the use of post conditions that depend on the state of the world at method entry, compared to that at the return. For instance, if you would have a class with some collection or list functionality, you could have an Add method. The Contract.OldValue<T>() method enables you to check the count at method exit compared to the count at method entry. The following example shows this:

    public void Add(T item)
    {
      Contract.Requires(item != null);
      Contract.Ensures(this.innerList.Count == Contract.OldValue<int>(this.innerList.Count) + 1);

      this.innerList.Add(item);
    }

The count at method exit should match the old count incremented with 1 for the added item. There may be other uses for the Contract.OldValue(). Just remember, it may only be used in Contract.Ensures calls, which limits the use to post conditional checking. Any other checks on pre-entry state should be handcrafted by the programmers themselves. Another way of checking object state is the use of the Contract.Invariant method calls in a private method marked with the ContractInvariantMethod attribute. A description of those will follow in some future post in this series. Next post will deal with the Contract.ValueAtReturn<T>() method call.

That’s it for now
Meile

Feedback

# re: Code Contracts: Contract.OldValue&lt;T&gt;() (8 of n)

2/25/2012 3:50 AM by discount oakley sunglasses
<a href="http://www.sunglasseshop2012.com/">discount oakley sunglasses</a> is a sport and lifestyle brand, driven to ignite the imagination through the fusion of art and science. The essence of the brand is communicated through hundreds of professional and amateur athletes who depend on <a href="http://www.sunglasseshop2012.com/">oakley">http://www.sunglasseshop2012.com/">oakley sunglasses outlet</a> products to provide them with the very best while they redefine what is physically possible.The <a href="http://www.sunglasseshop2012.com/">oakley">http://www.sunglasseshop2012.com/">oakley sunglasses</a> within our Era category represent the most recent technology and freshest new looks to fit your way of life.Since the 1940s sunglasses have been popular as a fashion accessory, especially on the beach.<a href="http://www.sunglasseshop2012.com/oakley-active-sunglasses-c-2.html">Oakley Active Sunglasses</a> that change colours with the ease of a chameleon could become the hottest fashion accessory.

<a href="http://www.sunglasseshop2012.com/new-oakley-sunglasses-c-1.html">Oakley Sunglasses 2012</a> has now been recognized as one of the most coveted brands in performance technology and fashion.<a href="http://www.sunglasseshop2012.com/oakley-lifestyle-sunglasses-c-3.html">Oakley Lifestyle Sunglasses</a> are famous for exceptional top quality technologies and a innovative range of <a href="http://www.sunglasseshop2012.com/oakley-sport-sunglasses-c-6.html">Oakley Sport Sunglasses</a>. But referring to the original one, the price is very high, some of us can not afford it, so <a href="http://www.sunglasseshop2012.com/oakley-multi-lens-sunglasses-c-4.html">Oakley Multi Lens Sunglasses</a> are you the best choice. We have been in wholesale Oakley sunglasses for 6 years, we always offer excellent quality and cheapest <a href="http://www.sunglasseshop2012.com/oakley-polarized-sunglasses-c-5.html">Oakley Polarized Sunglasses</a>.

# sac Lancel

3/2/2012 3:23 AM by sac Lancel
JTYYLHHKXOZDM
Trouver Sacs à main Lancel bon marché fabriqués en cuir de Lancel

# longchamp outlet

3/28/2012 8:18 AM by longchamp wallet
For longchamp online details on the core papers and other statistical characteristics of each Front, see the Research Fronts section of the Essential Science Indicators from Thomson Reuters. Some papers have comments sent in by the author(s) of the paper which may include Longchamp luxury bags images and descriptions of their work. http://www.longchampparis1948.com/

Post Comment

Title  
Name  
Url
Comment   

ATTENTION: the code you need to copy is CaSe SeNsItIvE and is required to prevent spam.
Enter the code you see:

posts - 24, comments - 125, trackbacks - 0, articles - 0

Copyright © Meile Zetstra