Creating code contract ForAll method

If this code contract sounds like it is validating some or the other collection, then you would be correct. The code contract ForAll will perform validation of IEnumerable collections. This is very handy, because as a developer, you do not need to do any kind of iteration over the collection and writing validation logic. This contract does it for you.

Getting ready

We will create a simple list of integers and populate the list with values. Our code contract will validate that the list does not contain any zero values.

How to do it…

  1. Before you go on, ensure that you have added the code contracts using statement to the top of your Recipes.cs class file:
    using System.Diagnostics.Contracts;
  2. Add a method called ValidateList() ...

Get C# Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.