Advertisement
Guest User

Untitled

a guest
Feb 15th, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.00 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics.Contracts;
  3.  
  4. namespace ContractsExample
  5. {
  6.     public class SimpleModel
  7.     {
  8.         public ShouldNotBeNull TestData { get; set; }
  9.  
  10.         public SimpleModel()
  11.         {
  12.             TestData = new ShouldNotBeNull {SomeData = 0};
  13.         }
  14.  
  15.         public void SomePerformances()
  16.         {
  17.             Contract.Requires<ArgumentNullException>(TestData != null);
  18.             Contract.Requires<ArgumentOutOfRangeException>(TestData.SomeData == 0);
  19.             Contract.Ensures(TestData.SomeData < 10);
  20.  
  21.             Console.WriteLine($"Number {TestData.SomeData}.");
  22.  
  23.             loopInvariantExample();
  24.         }
  25.  
  26.         private void loopInvariantExample()
  27.         {
  28.             for (int i = 0; i < 30; i++)
  29.             {
  30.                 TestData.SomeData++;
  31.             }
  32.         }
  33.  
  34.         [ContractInvariantMethod]
  35.         private void invariantValidation()
  36.         {
  37.             Contract.Invariant(TestData.SomeData < 10);
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement