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…
Before you go on, ensure that you have added the code contracts
usingstatement to the top of yourRecipes.csclass file:using System.Diagnostics.Contracts;
Add a method called
ValidateList()to your class and pass aList<int>collection to it:public static void ValidateList(List<int> lstValues) { }Inside the
ValidateList()method, add theContract.ForAllcontract. Interestingly, you will notice that we are using...