Instead of writing all the patterns as you use them, you can choose from a list of predefined patterns.
Life is what happens while you're busy making other plans.
Specifying patterns manually in each call can take quite some time, and make code somewhat convoluted. Furthermore, some patterns are used a lot, so they would be repeated across the code base.
Instead, the Patterns class offers a number of predefined patterns that can be used to save time.
To use one of these patterns, call its corresponding method, passing the required parameters if needed:
// Check whether a string contains a fairytale var isFairytale = Patterns.StartsWith("Once upon a time"); // Check the age of a person (inline) person.Age.Match(Patterns.IsGreaterThan(10), p => Console.WriteLine("Client does not qualify for discount."));
How to decide whether you need to explicitly write the generic parameters for a pattern.
In most cases, you don't need to specify the generic parameters for a pattern, because the compiler is able to infer them. That said, in some cases type inference is not possible.
The general rule is that if a pattern has at least a parameter of the same generic type as the matching object, type inference will work correctly.
In practice, refer to the compiler errors, or lack thereof, to determine whether a specific pattern requires type information.
It is our choices that show what we truly are.
These are the currently available patterns:
Pattern name | Applied to | Description |
---|---|---|
IsNull<T>() | Any class or nullable struct object. | Matches if the target object is null. |
IsEqualTo<T>(T target) | Any instance of IEquatable<T>. | Matches if the target object is equal to the parameter object. |
IsHeldWithin<T>(IEnumerable<T> target) | Any object. | Matches if the target object is contained by the parameter collection. |
IsGreaterThan<T>(T target) | Any instance of IComparable<T>. | Matches if the target object is greater than the parameter object. |
IsGreaterOrEqual<T>(T target) | Any instance of IComparable<T>. | Matches if the target object is greater or equal than the parameter object. |
IsLesserThan<T>(T target) | Any instance of IComparable<T>. | Matches if the target object is lesser than the parameter object. |
IsLesserOrEqual<T>(T target) | Any instance of IComparable<T>. | Matches if the target object is lesser or equal than the parameter object. |
IsWithinRange<T>(T lower, T upper) | Any instance of IComparable<T>. | Matches if the target object is between lower and upper (both inclusive). |
IsTrue() | Any boolean. | Matches if the target boolean is true. |
IsFalse() | Any boolean. | Matches if the target boolean is false. |
IsEmpty() | Any string. | Matches if the target string is empty. |
IsWhitespace() | Any string. | Matches if the target string contains only whitespace characters. |
Contains(string target) | Any string. | Matches if the target string contains the parameter string. |
IsLongerThan(int target) | Any string. | Matches if the target string is longer than the parameter length. |
IsShorterThan(int target) | Any string. | Matches if the target string is shorter than the parameter length. |
IsOfLength(int target) | Any string. | Matches if the target string has exactly the parameter length. |
StartsWith(string target) | Any string. | Matches if the target string starts with the parameter string. |
EndsWith(string target) | Any string. | Matches if the target string ends with the parameter string. |
IsEmpty<T>() | Any instance of ICollection<T>. | Matches if the target collection is empty. |
HasDuplicates<T>() | Any instance of ICollection<T>. | Matches if the target collection contains duplicated elements. |
IsSubsetOf<T>(ICollection<T> target) | Any instance of ICollection<T>. | Matches if the target collection is a subset of the parameter collection (that is, all its elements are contained by the parameter collection). |
IsSupersetOf<T>(ICollection<T> target) | Any instance of ICollection<T>. | Matches if the target collection is a superset of the parameter collection (that is, it contains all elements in the parameter collection). |
IsSequenceEqual<T>(ICollection<T> target) | Any instance of ICollection<T>. | Matches if the target collection contains the same elements in the same order as the parameter collection. |
ContainsAll<T>(ICollection<T> target) | Any instance of ICollection<T>. | Matches if the target collection contains the same elements as the parameter collection. |
Contains<T>(T target) | Any instance of ICollection<T>. | Matches if the target collection contains the parameter object. |
IsLongerThan<T>(int target) | Any instance of ICollection<T>. | Matches if the target collection is longer than the parameter length. |
IsShorterThan<T>(int target) | Any instance of ICollection<T>. | Matches if the target collection is shorter than the parameter lenght. |
IsOfLength<T>(int target) | Any instance of ICollection<T>. | Matches if the target collection has exactly the parameter length. |
IsBefore(DateTime target) | Any instance of DateTime. | Matches if the target DateTime is set before the parameter DateTime. |
IsAfter(DateTime target) | Any instance of DateTime. | Matches if the target DateTime is set after the parameter DateTime. |
You have learnt how to use the predefined patterns to save time and effort.