Eric Potter
Software Architect at Aptera
@pottereric
What C# Programmers
Need to Know About
Pattern Matching
What is Pattern
Matching?
Basic Structure More Powerful Structure
Iteration while
do..while
for
foreach
Selection if..else
switch..case
Basic Structure More Powerful Structure
Iteration while
do..while
for
foreach
Selection if..else
switch..case
Pattern matching
History
“We have now seen three types of decomposition;
we could call them "concatenation", "selection"
and "repetition" respectively. ”
– Edsger W. Dijkstra, Structured programming, 1972
“Once you strip away the syntactic sugar, our
programming languages essentially boil down
to three things: sequence, selection and
iteration, and every construct within those
languages is some combination of them.”
– Uncle Bob / Robert C Martin
Code
Old
Way
New
Way
Using
“When”
Using
“When”
Pattern Matching combines type validation,
variable evaluation and variable assignment.
Pattern Matching
TryParse
/ regex
is / as
switch
Pattern Matching
Evaluation
Assignment
Selection
Fizz Buzz Example
Real Examples
Other Languages
F#
Rust
C# 7.3 Proposal
https://github.com/dotnet/roslyn/blob/features/range/docs/features/range.md
Swift
Elixir
- Getting Started with Elixir by Nate Taylor (@taylonr)
Elixir
Elixir
When should you use
pattern matching?
• When you have different behaviors based on the type of an input variable,
consider using pattern matching with a Type Pattern.
• When you have a case statement with nested if blocks,
consider rewriting the blocks to have guards.
• When you have an "is" check immediately followed by an "as" cast,
consider using an "is" statement with pattern matching.
• When you have an as cast followed by a null check,
consider using an "is" statement with pattern matching.
Why should you care?
Pattern Matching
Evaluation
Assignment
Selection
Resources
• Blog: http://humbletoolsmith.com/tags/PatternMatching/
• Code: https://github.com/pottereric/PatternMatchingFizzBuzz
• Slides:
Thank You.
Let’s Chat Later.
Twitter: @pottereric
GitHub: pottereric
Blog: HumbleToolsmith.com

C# pattern matching

Editor's Notes

  • #2 Run ZoomIt Open C:\projects\PatternMatching\PatternMatchingExamples\PatternMatchingExamples.sln Open C:\projects\PatternMatching\FizzBuzz\FizzBuzz.sln Close the output window, open the test explorer Reference: https://blogs.msdn.microsoft.com/seteplia/2017/10/16/dissecting-the-pattern-matching-in-c-7/ Fix the spelling of fibbonaci, from/form “ResetFrom” TODO for next time – In the elixir example, show guards in the method overloads In the F# example, add a slide for active patterns Overall: more cool stuff from other languages ---- TODO – better Elixir example, one with guards TODO – see if the IL is shorter with Pattern matching
  • #3 “Reducio” (5A) C:\referenceProjects\roslyn\src\Compilers\CSharp\Portable\Compilation\CSharpSemanticModel.cs Bdc657ed Replacing as casts
  • #4 (5B)
  • #5 (5B)
  • #10 Having a new structure at this level is a big deal
  • #11 Having a new structure at this level is a big deal
  • #13 Sequence Selection Iteration Concatenation selection repetition conditional clause repetitive clause alternate clause
  • #15 C# Tuples demo – Fizz Buzz - Need to have System.ValueTuple nugget package C# Type Pattern demo F# - Completeness F# - matching arrays F# - matching lists Rust – matching ranges
  • #21 Sequence Selection Iteration Concatenation selection repetition conditional clause repetitive clause alternate clause
  • #23 Funnel with case, is, as, TryParse, and Regex
  • #24 Three headed monster, like fluffy
  • #25 The requirements of the problem are almost exactly laid out in the code Matching on Tuples makes a lot of sense because more complex objects benefit from more complex selection statements
  • #26 Now that Microsoft is writing open source code, we can look repositories like .Net Core, ASP.Net Core, and EF.Core and see how they are using pattern matching. All of these projects started before C# 7 was released. So in many cases we can see how code was written before and after pattern matching was introduced.
  • #27 (1A) C:\referenceProjects\EntityFramework\src\EFCore.Relational\Query\Sql\DefaultQuerySqlGenerator.cs Got rid of a null check
  • #28 (1B)
  • #29 (1C) No null reference check.
  • #30 (2A) C:\referenceProjects\EntityFramework\src\EFCore\Query\Internal\QueryOptimizer.cs Here we have a case with a when. The when clause uses the variable f022d66bc3a461851a6250536399cd3b8f8bf8d2 4/4/2017 9:37:15 PM
  • #31 (2B)
  • #32 (3A) C:\referenceProjects\EntityFramework\src\EFCore\Query\Internal\QueryOptimizer.cs Using the discard operator.
  • #33 (3B)
  • #34 (3C) Show the example with the discard operator.
  • #35 (4A) C:\referenceProjects\Mvc\src\Microsoft.AspNetCore.Mvc.TagHelpers\TagHelperOutputExtensions.cs Match on the class first (HtmlString) Match on an interface (IHtmlContent) HtmlString implements IHtmlContent
  • #36 (4B)
  • #37 (5A) C:\referenceProjects\roslyn\src\Compilers\CSharp\Portable\Compilation\CSharpSemanticModel.cs Bdc657ed Replacing as casts
  • #38 (5B)
  • #39 (6A) C:\referenceProjects\roslyn\src\Compilers\CSharp\Portable\Compilation\CSharpSemanticModel.cs Cast after the “is”
  • #40 (6B) Out variable after the “is” Pattern matching without the case Makes use of the declaration pattern [DeclarationPatternSyntax]
  • #41 (7A) C:\referenceProjects\roslyn\src\Compilers\CSharp\Portable\Parser\LanguageParser.cs Using a when clause with a constant pattern
  • #42 (7B)
  • #48 Take Off with Elixir – Rob Conery (book, tutorial combo)
  • #51 Pattern matching enables Cleaner code More expressive code Less code
  • #53 Pattern matching enables Cleaner code More expressive code Less code
  • #54 Cleaner code More expressive code Less code
  • #56 The PatternMatching tag on HT http://humbletoolsmith.com/tags/PatternMatching/