-
Notifications
You must be signed in to change notification settings - Fork 0
Contract
Artem Kirgizov edited this page May 31, 2021
·
6 revisions
Contract - a static class that throws exceptions under a negative condition. First of all, it is necessary in those places where the correctness of the condition is critical.
The main features of the class:
public void Foo(Tower value)
{
Contract.NotNull<Tower, ArgumentNullException>(value); // if value is null - throw ArgumentNullException
}
object[] objs = new object[]
{
new object(),
new object(),
new object(),
new object()
};
Foo(objs);
***
public void Foo(Tower value)
{
Contract.NotNull<object, ArgumentNullException>(objs); // if one of the values is null, an exception will be thrown
}
string str = "string";
Contract.StringFilled<ArgumentException>(str);
int first = 5;
int second = 5;
Contract.Equal<int, InvalidOperationException>(first, second); // if the values are not equal, an exception will be thrown.
Contract.NotEqual<int, InvalidOperationException>(first, second); // if the values are equal, an exception will be thrown.
int first = 5;
int second = 10;
Contract.MoreOrEqualThan<int, InvalidOperationException>(first, second); // thrown if the first is greater than or equal to the second value.
int first = 5;
Contract.Is<ArgumentException>(first == 0); // thrown ArgumentException because first is not 0.
int first = 5;
Contract.IsNot<ArgumentException>(first == 0); // ok.
- And others