Patterns and Its type
C# introduced pattern matching in C# 7.0. Since then, each major C# version extends pattern matching capabilities.
- Declaration pattern
- Type pattern
- Constant pattern
- Relational patterns
- Logical patterns
- Property pattern
- Positional pattern
- var pattern
- Discard pattern
Declaration pattern
With a declaration pattern, you can also declare a new local variable. When a declaration pattern matches an expression, that variable is assigned a converted expression result, as the following example shows:
Type pattern
Introduced in C# 9.0, Type pattern checks the run-time type of an expression as shown below:
Constant pattern
Introduced in C# 7.0, test if an expression result equals a specified constant as shown below:
Relational pattern
Beginning with C# 9.0, you use a relational pattern to compare an expression result with a constant, as the following example shows:
Logical pattern
Beginning with C# 9.0, you use the not, and, and or pattern combinators to create the following logical patterns:
Property pattern
Beginning with C# 8.0, you use a property pattern to match an expression's properties or fields against nested patterns, as the following example shows
Positional pattern
Beginning with C# 8.0, you use a positional pattern to deconstruct an expression result and match the resulting values against the corresponding nested patterns, as the following example shows:
Var pattern
Beginning with C# 7.0, you use a var pattern to match any expression, including null, and assign its result to a new local variable, as the following example shows
Discard pattern
Beginning with C# 8.0, you use a discard pattern _ to match any expression, including null, as the following example shows
No comments:
Post a Comment