https://developers.redhat.com/blog/2021/04/06/c-9-pattern-matching/ Red Hat Logo * Customer Portal * Developer * Hosted IDE * Marketplace * OpenShift * Partner Connect * Log In * * Log Out [logo] RHD Navigation * Build Link to resources for building applications with open source software * Tools Link to developer tools for cloud development * Events Link to Red Hat Developer Events * Training Link to Red Hat Developer Training Content * Partners Connect with Red Hat: Work together to build ideal customer solutions and support the services you provide with our products. * Products Link to Red Hat Developer Products * [ ] Close User account menu * Log in [ ] RHD Navigation * Build Link to resources for building applications with open source software * Tools Link to developer tools for cloud development * Events Link to Red Hat Developer Events * Training Link to Red Hat Developer Training Content * Products Link to Red Hat Developer Products * [ ] Menu Topic navigation * Istio * Quarkus * Kubernetes * CI/CD * Serverless * Java * Linux * Microservices * DevOps * All Topics All Topics Blog Articles C# 9 pattern matching tdeseyn By Tom Deseyn April 6, 2021April 5, 2021 C# 9 pattern matching The previous article in our C# 9 series looked at top-level programs and target-typed expressions. In this article, we'll cover new features for pattern matching. You can find an overview of the syntax offered by previous versions of C# in C# 8 pattern matching. Type patterns When checking against a type, previous versions of C# required you to include a variable name (or a _ discard). This is no longer required with C# 9: // is pattern with Type if (input is Person) ... // case pattern with Type switch (input) { case Person: ... // is pattern with tuple Type if (input is (int, string)) ... [2021_RHD_P] Combining patterns With the is expression in earlier versions of C#, you could already combine patterns using regular logical operators: if (person is Student || person is Teacher) ... However, this doesn't work for switch expressions and switch case labels. C# 9 adds support for combining patterns using the and and or keywords, which works for both if and switch: if (person is Student or Teacher) ... decimal discount = person switch { Student or Teacher => 0.1m, _ => 0 }; switch (person) { case Student or Teacher: ... The and patterns have higher precedence than the or patterns. You can add parentheses to change or clarify the precedence. Inverting patterns With C# 9, you can invert patterns using the not keyword: if (person is not Student) ... switch (person) { case not Student: ... An interesting case is the is not null pattern. This will check whether the reference is not null. Using != null may check something different when the type overloads the != operator. if (person is not null) ... Relational patterns Relational patterns allow you to compare an expression to a constant numeric value: decimal discount = age switch { <= 2 => 1, < 6 => 0.5m, < 10 => 0.2m, _ => 0 }; Patterns within patterns Patterns can also contain other patterns. This nesting lets you express complex conditions in a concise and readable way. The following example combines several types of patterns: if (person is Student { Age : >20 and <30 }) ... Conclusion In this article, we looked at the new pattern matching features in C# 9. The additions allow you to express more complex conditions with a clear, concise syntax. C# 9 can be used with the .NET 5 SDK, which is available on Red Hat Enterprise Linux and Red Hat OpenShift, on Fedora, and from Microsoft for Windows, macOS, and other Linux distributions. Related Share Categorized In .NET, C#, Linux Tags .NET 5 C# 9 Recent Posts * Deploy Quarkus everywhere with Red Hat Enterprise Linux (RHEL) * Securely connect Red Hat Integration Service Registry with Red Hat AMQ Streams * C# 9 pattern matching * Get started with clang-tidy in Red Hat Enterprise Linux * Testing Apicurio Registry's performance and scalability * Design event-driven integrations with Kamelets and Camel K * Deploy integration components easily with the Red Hat Integration Operator * Get started with XDP * Security and management improvements in Red Hat JBoss Enterprise Application Platform 7.4 Beta * C# 9 top-level programs and target-typed expressions Top Posts * How to install Python 3 on Red Hat Enterprise Linux * Top 10 must-know Kubernetes design patterns * How to install Java 8 and 11 on Red Hat Enterprise Linux 8 * Introduction to Linux interfaces for virtual networking * Podman and Buildah for Docker users Feeds RSSATOM Login to admin your blog * Featured Topics + Istio + CI/CD + Serverless + Enterprise Java + Linux + Microservices + DevOps * Build + Getting Started Center + Developer Tools + Hosted Che IDE + Interactive Tutorials + Container Catalog + Operators Marketplace + Certify Applications + Red Hat on Github * Quicklinks + What's new + DevNation events + Upcoming Events + Books + Cheat Sheets + Videos + Products * Communicate + Site Status Dashboard + Report a website issue + Report a security problem + Helping during COVID-19 + About us + Contact Sales Red Hat Developer Build here. Go anywhere. We serve the builders. The problem solvers who create careers with code. Join us if you're a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead. * * * * Sign me up Red Hat Logo * Copyright (c) 2018 Red Hat Inc. + + Privacy Statement + Terms of Use + All policies and guidelines Red Hat Summit Privacy Preference Center Options * Consent Management * Cookie Settings + Necessary + Analytics + Other * Privacy Policy Consent Management We use cookies on our websites to deliver our online services. Details about how we use cookies and how you may disable them are set out in our Privacy Statement. By using this website you agree to our use of cookies. Privacy Policy Required We use cookies on our websites to deliver our online services. Details about Red Hat's privacy policy, how we use cookies and how you may disable them are set out in our Privacy Page. By using this website you agree to our use of cookies. Necessary For ensuring site stability and functionality. Cookies Used Required __CT_Data, _CT_RS_, BIGipServer~prod~rhd-blog-http, check,dmdbase_cdc, gdpr[allowed_cookies], gdpr[consent_types], sat_ppv,sat_prevPage,WRUID,atlassian.xsrf.token, JSESSIONID, DWRSESSIONID Analytics For site visitor traffic analysis developers.redhat.com Opt Out _sdsat_eloquaGUID,AMCV_945D02BE532957400A490D4CAdobeOrg, rh_omni_tc, s_sq, mbox, _sdsat_eloquaGUID,rh_elqCustomerGUID Other Disqus is used to facilitate comments on individual blog posts. disqus.com Opt Out G_ENABLED_IDPS,NID,__jid,cpSess,disqus_unique,io.narrative.guid.v2,uuid2,vglnk.Agent.p,vglnk.PartnerRfsh.p [Save Preferences] OK