SlideShare a Scribd company logo
Eric Potter
Software Architect at Aptera
@pottereric
What C# Programmers
Need to Know About
Pattern Matching
C# pattern matching
C# pattern matching
C# pattern matching
C# pattern matching
C# pattern matching
C# 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
C# pattern matching
Using
“When”
Using
“When”
Pattern Matching combines type validation,
variable evaluation and variable assignment.
C# pattern matching
Pattern Matching
TryParse
/ regex
is / as
switch
Pattern Matching
Evaluation
Assignment
Selection
Fizz Buzz Example
Real Examples
C# pattern matching
C# pattern matching
C# pattern matching
C# pattern matching
C# pattern matching
C# pattern matching
C# pattern matching
C# pattern matching
C# pattern matching
C# pattern matching
C# pattern matching
C# pattern matching
C# pattern matching
C# pattern matching
C# pattern matching
C# pattern matching
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

More Related Content

PPTX
Object Oriented Programming
PPT
Design Patterns
PDF
Beyond design patterns phpnw14
PPTX
Optimizing Application Architecture (.NET/Java topics)
PPT
Lunch and learn as3_frameworks
PDF
Essential java script design patterns
PDF
Code as Data workshop: Using source{d} Engine to extract insights from git re...
PDF
SOLID Principles and Design Patterns
Object Oriented Programming
Design Patterns
Beyond design patterns phpnw14
Optimizing Application Architecture (.NET/Java topics)
Lunch and learn as3_frameworks
Essential java script design patterns
Code as Data workshop: Using source{d} Engine to extract insights from git re...
SOLID Principles and Design Patterns

Similar to C# pattern matching (20)

PPTX
Apex for humans
PPTX
The Triplex Approach for Recognizing Semantic Relations from Noun Phrases, Ap...
PPTX
apna ppt 2.pptx
PDF
CS4200 2019 Lecture 1: Introduction
PDF
Design Patterns from 10K feet
PPTX
NDC Sydney 2019 - Microservices for building an IDE – The innards of JetBrain...
PPTX
mbeddr meets IncQuer - Combining the Best Features of Two Modeling Worlds
PPTX
Software design and Architecture.pptx
PPT
Design patterns represent the best practices used by experienced object-orien...
PPT
10-DesignPatterns.ppt
PPTX
API Workshop: Deep dive into code samples
PPT
phases of compiler PPT includes phases of compiler
PPT
Evolving as a professional software developer
PDF
Programming style
PPTX
Factory Design Pattern
PDF
Writing Readable Code
PPTX
Basic Fundamentals Concept of Programming.pptx
PDF
Are High Level Programming Languages for Multicore and Safety Critical Conver...
PPTX
PPTX
Google code search
Apex for humans
The Triplex Approach for Recognizing Semantic Relations from Noun Phrases, Ap...
apna ppt 2.pptx
CS4200 2019 Lecture 1: Introduction
Design Patterns from 10K feet
NDC Sydney 2019 - Microservices for building an IDE – The innards of JetBrain...
mbeddr meets IncQuer - Combining the Best Features of Two Modeling Worlds
Software design and Architecture.pptx
Design patterns represent the best practices used by experienced object-orien...
10-DesignPatterns.ppt
API Workshop: Deep dive into code samples
phases of compiler PPT includes phases of compiler
Evolving as a professional software developer
Programming style
Factory Design Pattern
Writing Readable Code
Basic Fundamentals Concept of Programming.pptx
Are High Level Programming Languages for Multicore and Safety Critical Conver...
Google code search
Ad

More from Eric Potter (8)

PPTX
Learning Go From Hello World to Web Service.pptx
PPTX
Learning Go From Hello World to Web Service.pptx
PPTX
KnowYouLimitations.pptx
PPTX
KnowYouLimitations.pptx
PPTX
Easy Automated UI Testing with Canopy
PPTX
Finding Your Place in the Cosmos - Azure Cosmos DB
PPTX
TypeScript: Beyond The Basics
PPTX
Generalist or Specialist
Learning Go From Hello World to Web Service.pptx
Learning Go From Hello World to Web Service.pptx
KnowYouLimitations.pptx
KnowYouLimitations.pptx
Easy Automated UI Testing with Canopy
Finding Your Place in the Cosmos - Azure Cosmos DB
TypeScript: Beyond The Basics
Generalist or Specialist
Ad

Recently uploaded (20)

PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Digital Strategies for Manufacturing Companies
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
medical staffing services at VALiNTRY
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
history of c programming in notes for students .pptx
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
System and Network Administration Chapter 2
PPTX
Essential Infomation Tech presentation.pptx
How to Choose the Right IT Partner for Your Business in Malaysia
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Wondershare Filmora 15 Crack With Activation Key [2025
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Understanding Forklifts - TECH EHS Solution
Softaken Excel to vCard Converter Software.pdf
Design an Analysis of Algorithms II-SECS-1021-03
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Odoo Companies in India – Driving Business Transformation.pdf
Digital Strategies for Manufacturing Companies
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
medical staffing services at VALiNTRY
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
history of c programming in notes for students .pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
System and Network Administration Chapter 2
Essential Infomation Tech presentation.pptx

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/