03.01.2015 Views

C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

Visual Studio 2013 C# 5.0 Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

212 ❘ CHAPTER 9 Error Handling<br />

Figure 9-1: Use the Code Contracts property page to turn on code contracts.<br />

Now whenever the program executes a public method from outside of the class’s code, it checks the<br />

invariant after that method call returns to make sure the invariant property is still true.<br />

For example, suppose the Order class has a Customer property and that property should never be<br />

null throughout the lifetime of the Order object. The following code shows how you might use a<br />

contract to enforce that condition in the Order class.<br />

class Order<br />

{<br />

public List Items = new List();<br />

public Customer Customer { get; set; }<br />

public Order(Customer customer)<br />

{<br />

Customer = customer;<br />

}<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!