# Using Pattern Matching

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&#x20;
* 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:

```fsharp
match boolExpression with
| true  -> //pattern when true 
| false -> //pattern when false
```

You can also using patterns to compare an `int` value:

```fsharp
let value = 10
match value with
| 1 | 2 | 3         -> //When the value is 1, 2 or 3
| n when n % 2 = 0  -> //When the value is an even number
| 5                 -> //When the value is equals to 5
| _                 -> //Other cases
```

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 [Microsoft](https://docs.microsoft.com/pt-br/dotnet/csharp/pattern-matching).

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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gabriel-schade-cardoso.gitbook.io/tango/fundamentals/pattern-matching.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
