The evolution of Pattern Matching in C# (from version 6 to 10)

  • 🎬 Video
  • ℹ️ Published 2 years ago
preview_player
UCrkPsvLGln62OMZRO6K-llg

Join me and let's build the future of payments!


Hello everybody I'm Nick and in this video I will show you how pattern matching was introduced in C# and how it has evolved from version 6 all the way to version 10. It is an amazing feature which can be really handy to prevent you having to write lengthy complicated clauses.

Timestamps
Intro - 0:00
I am hiring! - 0:47
C# 6 - 1:37
C# 7 - 3:03
C# 8 - 5:36
C# 9 - 9:31
C# 10 - 13:58

Don't forget to comment, like and subscribe :)

Social Media:


#csharp #dotnet

💬 Comments
Author

Join me and let's build the future of payments!

Author — Nick Chapsas

Author

It is becoming really hard to try other languages when your first and favorite language is giving you these awesome features. And .NET is in everything in everywhere.. Thank you Nick for showing this awesome feature

Author — John Paderon

Author

IMHO:
One thing that would definitely improve pattern matching is to *allow us to use other values besides constants.* As it stands, pattern matching literally forces us to use hard coded magic numbers if we want to do anything more than check types. The biggest reason I'm not using pattern matching as much as I'd love to is because way more often than not I need to match against VARIABLES, not constants. Right now you can't even do a switch expression on a Type object for crying out loud.

I know that one argument against matching against variables is that then the compiler wouldn't be able to (attempt to) guarantee static analysis, but I don't see a problem in not having it when using variables.

Seriously, variables in pattern matching have got to be the next step if people are to use it more.

Even in the context of something similar to the example, if you're branching based on some shape's geometrical properties in a real application, then you're gonna use, say, a float you got as a method argument, not magic numbers.

Author — Petrusion

Author

When I moved from VB6 to C# 15 years ago, I was shocked at how limited switch case was vs Select Case in the obsolete VB and VBA languages. Now C# has FINALLY surpassed Visual Basic in functionality and even readability with that extremely short syntax. Totally awesome, thank you for waking me up to this new future set!

Author — Ian Crane

Author

I really appreciate you doing these videos. You're making us better coders 😀

Author — Keith Williamson

Author

Since they are advancing the pattern matching this much, I really wish they would introduce uniontypes etc. similar to typescript - it just seems it would be a good match, making it possible to e.g. return different types in a method, and the caller can then choose to handle this using pattern matching with type checks for the more limited amount of possible types (opposed to using e.g. object as return object).

Author — jhdk356

Author

I'm not saying it's crazy. But, well, it is crazy! Amazing evolution! Thank you for share that, really a very nice job, another usefull tip, as always!

Author — José Luís de Oliveira Santos

Author

Great summary of a feature I have not be using a lot, since in "oldskool software engineering" dynamic type checking is considered to be a bit of a code smell. But still, I can see the benefits of this "pattern matching", although the name is really confusing to me : I consider this to be "dynamic type matching", and e.g. regular expression matching is what I call "pattern matching".

Author — Bart Blommerde

Author

Nick, excellent work. You really distilled pattern matching down into the most concise example I have seen to date. Very well explained and even taught me something new. Bravo!

Author — qtx

Author

Nice presentation! Property matching is a really cool feature, however they only work with const values. It won't work if you need to check against another object, at least not yet.

Author — twiksify

Author

Started learning Haskell recently, so this is extremely interesting.
Do you know if there is a way to utilise a Predicate directly in the is {} syntax? Or do I absolutely have to make it in a separate condition(Ex: s is {} && myPredicate(s.Width))?
For example:
if (s is Rectangle { Width: Equals42 })
Where "Equals42", is a predicate that will receive a width in parameter and return true if it's 42, and false otherwise.

Author — The Round Table

Author

This is definitely useful for me.

I learn something from every video. Thanks for the great stuff you put up.

Author — Coder B

Author

When I first seen this back in C#8 I thought this was very powerful, but in practice there's not as many cases where I can use it in day-to-day code.

Author — ziplock9000

Author

Hi Nick, thanks for another interesting video. I am wondering, isn't dangerous to have two switch conditions about different properties of an object as both of the conditions can be true at the same time?
And what is happening here in the compiler code? Just nested if else statements?

Author — Daniël Tulp

Author

For a lot of these more "flavorful" features in other languages, I feel they're overly specific and cause more problems than they solve (looking at you, CSS). This seems fantastic, though. It's clear, concise, and customizable. Great video, thanks for the tips!

Author — Amy A

Author

My passion of c# is increasing thanks to your videos Nick 😉

Author — Najib Mestaoui

Author

I knew the power of pattern matching in the switch expressions, but I had no idea you could do it in an if statement. That's awesome! Question though.... how could you (if at all possible) do pattern matching on a class property where you want to do .StartsWith or something like that? So I want to do something like - if (vehicle is Car { color: StartsWith("Blue") }), where color is a string. I know that's an oddball example, just trying to show what I'm asking :)

Author — Brian M

Author

Ok, this really affects you if you are focused on doing functional programming with a lot of expressions. Code shown in the examples is harder to understand and change than “oldschool” if/else or switches. It has it purposes but it also has its limitations. Be vary with implementation, not everything is suitable everywhere. That aside great presentation and very interesting to watch! Kind regards.

Author — Zoran Bošnjak

Author

Είσαι τρομερός! great to see people like you sharing such valuable knowledge, keep up the good work!

Author — Χριστόφορος Πετρολιάς

Author

Switch expressions with tuples under the right circumstances can make for crazy improvements to readability and conciseness. Awesome video.

Author — D.K.S