mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-04-08 11:56:41 +00:00
Improve pattern matching and switch notes
This commit is contained in:
parent
02c0a04aad
commit
29c6b2a5c7
1 changed files with 31 additions and 53 deletions
|
@ -796,6 +796,35 @@ else
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Pattern Matching
|
||||||
|
|
||||||
|
[Pattern Matching](https://docs.microsoft.com/en-us/dotnet/csharp/pattern-matching)
|
||||||
|
|
||||||
|
A pattern describes one or more criteria that a value can be tested against. It's usable in switch statements, switch expressions and if statements.
|
||||||
|
|
||||||
|
```cs
|
||||||
|
<expr> is Type t // type pattern
|
||||||
|
<expr> is (Type X, Type Y): // positional pattern
|
||||||
|
<expr> is (Type X, _): // positional pattern + discard pattern
|
||||||
|
<expr> is Type {Property: value}: // property patten (check Type and that the property has a certain value)
|
||||||
|
<expr> is Type {Property: value} p: // property patten with output (variable p is usable in the block)
|
||||||
|
|
||||||
|
// constant pattern
|
||||||
|
<expr> is literalValue // e.g. 1, 'c', "literal"
|
||||||
|
<expr> is CONSTANT
|
||||||
|
<expr> is Enum.Value
|
||||||
|
|
||||||
|
// C# 9+
|
||||||
|
<expr> is pattern and pattern
|
||||||
|
<expr> is pattern or pattern
|
||||||
|
<expr> is not pattern
|
||||||
|
<expr> is { Property: > value } // works with all comparison operators
|
||||||
|
<expr> is > value // works with all comparison operators
|
||||||
|
|
||||||
|
// C# 10+
|
||||||
|
<expr> is { Property.InnerProperty: value } // match in nested properties
|
||||||
|
```
|
||||||
|
|
||||||
### Switch
|
### Switch
|
||||||
|
|
||||||
The `when` keyword can be used to specify a filter condition that causes its associated case label to be true only if the filter condition is also true.
|
The `when` keyword can be used to specify a filter condition that causes its associated case label to be true only if the filter condition is also true.
|
||||||
|
@ -819,25 +848,7 @@ switch (expr)
|
||||||
// code here
|
// code here
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Type t: // if expr matches Type result is stored in t local variable to be used in the block
|
// cases using pattern matching
|
||||||
// code here
|
|
||||||
break;
|
|
||||||
|
|
||||||
case value when(condition):
|
|
||||||
// code here
|
|
||||||
break;
|
|
||||||
|
|
||||||
case < value:
|
|
||||||
// code here
|
|
||||||
break;
|
|
||||||
|
|
||||||
case <= value and >= value:
|
|
||||||
// code here
|
|
||||||
break;
|
|
||||||
|
|
||||||
case > value:
|
|
||||||
// code here
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// code here
|
// code here
|
||||||
|
@ -849,43 +860,10 @@ variable switch
|
||||||
{
|
{
|
||||||
key_1 => value,
|
key_1 => value,
|
||||||
key_2 => value,
|
key_2 => value,
|
||||||
|
// cases using pattern matching
|
||||||
...
|
...
|
||||||
_ => default_value // underscore (_) discard pattern as default case
|
_ => default_value // underscore (_) discard pattern as default case
|
||||||
};
|
};
|
||||||
|
|
||||||
Type func(Type input){
|
|
||||||
return input switch
|
|
||||||
{
|
|
||||||
key_1 => value,
|
|
||||||
key_2 => value,
|
|
||||||
...,
|
|
||||||
_ => default_value // underscore (_) discard pattern as default case
|
|
||||||
};
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Pattern Matching
|
|
||||||
|
|
||||||
[Pattern Matching](https://docs.microsoft.com/en-us/dotnet/csharp/pattern-matching)
|
|
||||||
|
|
||||||
A pattern describes one or more criteria that a value can be tested against.
|
|
||||||
|
|
||||||
```cs
|
|
||||||
<expr> is Type t // type pattern
|
|
||||||
<expr> is (Type X, Type Y): // positional pattern
|
|
||||||
<expr> is (Type X, _): // positional pattern + discard pattern
|
|
||||||
<expr> is Type {Property : value}: // property patten (check Type and that the property has a certain value)
|
|
||||||
<expr> is Type {Property : value} p: // property patten with output (variable p is usable in the block)
|
|
||||||
|
|
||||||
// constant pattern
|
|
||||||
<expr> is literalValue // e.g. 1, 'c', "literal"
|
|
||||||
<expr> is CONSTANT
|
|
||||||
<expr> is Enum.Value
|
|
||||||
|
|
||||||
// C# 9+
|
|
||||||
<expr> is pattern and pattern
|
|
||||||
<expr> is pattern or pattern
|
|
||||||
<expr> is not pattern
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Loop Statements
|
## Loop Statements
|
||||||
|
|
Loading…
Add table
Reference in a new issue