Using Pattern Matching
Last updated
Was this helpful?
Last updated
Was this helpful?
Pattern Matching is one of the fundamental concepts of functional programming.
This concept can be used to replace a lot of code flow structures, such as:
If-else statements
Switch-cases
Loops (for, foreach, while e do)
Types comparison
A pattern matching in the F# language to replace an if statement has a syntax similar to:
You can also using patterns to compare an int
value:
There are a lot of benefits to using pattern matching, so, from C# 7.0 version it is possible to use this feature natively in the language, according to the documentation of the .
As an implementation in Tango library, some types have a method called Match
.
These methods receive lambda expressions for each pattern as a parameter, according to each type.
Besides that, there are also an implementation of a method called Match2
, similar to the previous one, but in this case, the patterns are applied to two different values simultaneously.