This is the online documentation for PostSharp 5.0.
Download PDF or CHM. Go to v4.3 or v5.0

Contracts

Throwing exceptions upon detecting a bad or unexpected value is good programming practice called precondition checking. However, writing the same checks over and over in different areas of the code base is tedious, error prone, and difficult to maintain.

PostSharp Code Contracts have the following features and benefits:

  • More readable. PostSharp Code Contracts are represented as custom attributes there is less code to read and understand.

  • Inherited. You can add a PostSharp Code Contract attribute to an interface method parameter and it will automatically be enforced in all implementations of this method.

  • Localizable. It's easy to display the error message in the user's language, even if you didn't design for this scenario upfront.

In this chapter

Section

Description

Adding Contracts to Code

This section demonstrates how to add contracts to code and how inheritance works.

Creating Custom Contracts

This section explains how to create your own contract attributes.

Localizing Contract Errors

This section describes how to customize the texts of exceptions that are thrown when a contract is violated.

See Also