{"id":70272,"date":"2022-06-15T08:03:08","date_gmt":"2022-06-15T06:03:08","guid":{"rendered":"https:\/\/drafts.code-maze.com\/?p=70272"},"modified":"2022-06-15T08:03:08","modified_gmt":"2022-06-15T06:03:08","slug":"csharp-pattern-matching","status":"publish","type":"post","link":"https:\/\/code-maze.com\/csharp-pattern-matching\/","title":{"rendered":"Pattern Matching in C#"},"content":{"rendered":"<p>In this article, we are going to talk about pattern matching in C#. We&#8217;ll see different patterns in action with simple examples.\u00a0<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/csharp-basic-topics\/Patterns\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>Let&#8217;s take a look at seven different types of pattern matching in C#!<\/p>\n<h2>What is Pattern Matching?<\/h2>\n<p>Essentially with pattern matching, we can check if two things are equivalent.<\/p>\n<p>But why is this useful?\u00a0 One example includes allowing us to make decisions based on the type of something rather than the value of something.<\/p>\n<p>Prior to pattern matching, we first would need to validate a condition to determine the object&#8217;s type.\u00a0 After discovering the type, we would have to perform a cast so we could treat the object accordingly. However, utilizing pattern matching in C#, we can make a decision on whether to use the object based on the type alone!<\/p>\n<p>Additionally, pattern matching is not limited to type matching, but can be applied in a variety of different contexts that C# has included since version 7.0 by mainly utilizing the <a href=\"https:\/\/code-maze.com\/csharp-conditions-if-else-swtich-case\/\" target=\"_blank\" rel=\"noopener\">switch statement<\/a>, <a href=\"https:\/\/code-maze.com\/csharp-as-is-operators\/\" target=\"_blank\" rel=\"noopener\">is operator<\/a>, or <a href=\"https:\/\/code-maze.com\/csharp-switch-multiple-cases-return-same-result\/\" target=\"_blank\" rel=\"noopener\">switch expression<\/a>.<\/p>\n<p>In short, we can take lengthy nested <code>if<\/code> statements and turn them into a few clean readable lines of code.<\/p>\n<h2><a id=\"Type\"><\/a>Type Patterns<\/h2>\n<p>Type patterns check to see if the given expression is not null. If not null, then the pattern checks to determine whether the expression is the same or can be cast to the same specified type.<\/p>\n<p>For our pattern matching examples, we will be using a custom abstract object <code>Animal<\/code> that we will implement by various custom classes (i.e. <code>Cat<\/code>, <code>Dog<\/code>, etc.):<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">private static string MatchTypePattern(Animal animal) =&gt; animal switch\r\n{\r\n    Cat =&gt; \"Meow\",\r\n    Dog =&gt; \"Bark\",\r\n    null =&gt; \"Please Provide A Non-Null Animal Object!\",\r\n    _ =&gt; $\"Unknown Animal {nameof(animal)}!\"\r\n};<\/pre>\n<p>Based on the type of object, we return a string message. Notice how we can gracefully determine whether the object provided is null or is one of the <code>Animal<\/code> implementations.\u00a0 Remember that we can use the &#8220;discard&#8221; symbol <code>_<\/code> to match anything else that we did not list &#8211; including null.<\/p>\n<p>To use the method we can initialize a new <code>Cat<\/code> object and call <code>MatchTypePattern<\/code>:\u00a0<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var message = MatchTypePattern(new Cat());<\/code><\/p>\n<p>Note that we could use this same method to assign a result to a declared variable also known as a <em>Declaration Pattern.<\/em><\/p>\n<h2><a id=\"Constant\"><\/a>Constant Pattern<\/h2>\n<p>We use a constant pattern to perform an equality check with a stored value against a set of values:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">private static string MatchConstantPattern(string animalmessage) =&gt; animalmessage switch\r\n{\r\n    \"Meow\" =&gt; \"Cat\",\r\n    \"Bark\" =&gt; \"Dog\",\r\n    null =&gt; \"Please Provide A Non-Null Message!\",\r\n    _ =&gt; $\"Unknown Animal Message {animalmessage}!\"\r\n};<\/pre>\n<p>This pattern matches the value and not the type (as the <em>Type Pattern does<\/em>).\u00a0 Again, we were able to gracefully handle an exact match for whatever string we passed to the method while also checking for null and handling unexpected inputs.<\/p>\n<p>We can call <code>MatchConstantPattern<\/code> by supplying a string message as our parameter to be matched against the constants we listed:\u00a0<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var name = MatchConstantPattern(\"Bark\");<\/code><\/p>\n<h2><a id=\"Relational\"><\/a>Relational Patterns<\/h2>\n<p>Relational patterns allow us to use comparison operators (such as<code> &lt; &gt; &lt;= &gt;=<\/code>) to determine if the current value matches the given comparison expression:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">private static string MatchRelationalPattern(int? animalcount) =&gt; animalcount switch\r\n{\r\n    &lt; 1000 =&gt; \"Less than one thousand animals\",\r\n    &gt;= 1000 =&gt; \"Greater than or equal to one thousand animals\"\r\n};<\/pre>\n<p>If the number of animals is within the specified range, the expression returns the corresponding string message.<\/p>\n<p>We can then call the method which returns the <code>\"Less than two animals\"<\/code> message:\u00a0<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">MatchRelationalPattern(1);<\/code><\/p>\n<h2><a id=\"Logical\"><\/a>Logical Patterns<\/h2>\n<p>We can incorporate logical patterns in conjunction with the other patterns mentioned in this article. With logical patterns, we can use <code>and<\/code>, <code>or<\/code>, and <code>not<\/code> operators within the pattern expressions. The simplest example is performing a null check so we can call the <code>MatchRelationPattern<\/code> method safely:\u00a0<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">if(numberOfAnimals is not null) MatchRelationalPattern(numberOfAnimals);<\/code><\/p>\n<p>Furthermore, we can extend a relational pattern to better match whatever pattern we desire:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">private static string MatchLogicalPattern(int animalage) =&gt; animalage switch\r\n{\r\n    0 or (&gt; 0 and &lt; 5) =&gt; \"Baby animal\",\r\n    &gt;= 5 and &lt;= 13 =&gt; \"Child animal\",\r\n    &gt; 13 and &lt; 20 =&gt; \"Teenage animal\",\r\n    &gt;= 20 and &lt; 60 =&gt; \"Adult animal\",\r\n    &gt;= 60 =&gt; \"Senior animal\",\r\n    _ =&gt; \"Unborn animal\"\r\n};<\/pre>\n<p>We can now match against multiple specified ranges and not limit ourselves to matching a specific constant! Remember that the order of operations for resolving the logical operators within the same expression is <code>not<\/code>, <code>and<\/code>, <code>or<\/code> respectively. However, we can use parenthesis to change the order of any of the comparisons (as shown in the &#8220;Baby Animal&#8221; case).<\/p>\n<p>We can use this method in a very similar manner as we did so far, which would return the <code>\"Adult animal\"<\/code> message:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">MatchLogicalPattern(20);<\/code><\/p>\n<h2><a id=\"Property\"><\/a>Property Pattern<\/h2>\n<p>We can use a property pattern to check a non-null object and its specific properties to match our specified criteria:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">private static bool MatchPropertyPattern(Animal animal) =&gt; animal is\r\n{\r\n    Name: \"Dog\", Description: \"furry animal with tail and paws\", Cloned: false or true\r\n};<\/pre>\n<p>Notice how we can simply list as many comma-separated properties of the object that we want to check. Additionally, we can include the logical operators to expand the criteria for our pattern (as shown for the <code>Cloned<\/code> property). If any one of the checks fails, then the entire expression returns false.<\/p>\n<p>By simply providing a new Dog instance the <code>MatchPropetyPattern<\/code> method returns true:\u00a0<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">MatchPropertyPattern(new Dog());<\/code><\/p>\n<h2><a id=\"Positional\"><\/a>Positional Pattern<\/h2>\n<p>We can utilize a positional pattern to take a result and match it positionally against multiple values:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">private static string MatchPositionalPattern(int animalage, int animalweight) =&gt; (animalage, animalweight) switch\r\n{\r\n    ( &lt;= 20, &lt;= 50) =&gt; \"Healthy young animal weight\",\r\n    ( &lt;= 20, &gt; 50) =&gt; \"Unhealthy young animal weight\",\r\n    ( &gt;= 21, &lt;= 100) =&gt; \"Healthy adult animal weight\",\r\n    ( &gt;= 21, &gt; 100) =&gt; \"Unhealthy adult animal weight\"\r\n};<\/pre>\n<p>We can take the given parameters of the method and turn them into a tuple that we can compare at one time. See how we use this pattern to remove the need for additional code that we would need to check each value separately?<\/p>\n<p>Calling the <code>MatchPositionalPattern<\/code> gives us the result of <code>\"Unhealthy adult animal weight\"<\/code>:\u00a0<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">MatchPositionalPattern(50, 10000);<\/code><\/p>\n<h2><a id=\"Var\"><\/a>Var Pattern<\/h2>\n<p>The var pattern temporarily holds and checks any values that we need within the expression:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static bool MatchVarPattern(Animal animal) =&gt; \r\n            animal.CreateClone() is var clone \r\n            &amp;&amp; clone.Clone \r\n            &amp;&amp; animal.Cloned\r\n            &amp;&amp; animal.GetType() == clone.GetType();<\/pre>\n<p>The type of var is determined at compile time and will be the result of the <code>Animal CreateClone<\/code> method.\u00a0 We can then perform checks on the result of what we stored from the <code>CreateClone<\/code> method!<\/p>\n<p>Calling this method will return a boolean as to whether the clone was successful:\u00a0<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\"> var cloned = MatchVarPattern(new Dog());<\/code><\/p>\n<h2>Conclusion<\/h2>\n<p>We have now gone through seven different types of pattern matching in C#!\u00a0 We hope that going forward, this knowledge will help you to write code that is more readable and maintainable thanks to this modern approach to dealing with patterns.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we are going to talk about pattern matching in C#. We&#8217;ll see different patterns in action with simple examples.\u00a0 Let&#8217;s take a look at seven different types of pattern matching in C#! What is Pattern Matching? Essentially with pattern matching, we can check if two things are equivalent. But why is this [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":62189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12],"tags":[1039,1305,1302,1306,1041,1303,1304],"class_list":["post-70272","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","tag-constant-pattern","tag-logical-pattern","tag-pattern-matching","tag-property-pattern","tag-relational-pattern","tag-type-patterns","tag-var-pattern","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Pattern Matching in C# - Code Maze<\/title>\n<meta name=\"description\" content=\"How to utilize pattern matching in C# using the following patterns: Type, Constant, Relational, Logical, Property, Positional, and Var pattern\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/csharp-pattern-matching\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Pattern Matching in C# - Code Maze\" \/>\n<meta property=\"og:description\" content=\"How to utilize pattern matching in C# using the following patterns: Type, Constant, Relational, Logical, Property, Positional, and Var pattern\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/csharp-pattern-matching\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2022-06-15T06:03:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Code Maze\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Code Maze\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/csharp-pattern-matching\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/csharp-pattern-matching\/\"},\"author\":{\"name\":\"Code Maze\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\"},\"headline\":\"Pattern Matching in C#\",\"datePublished\":\"2022-06-15T06:03:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-pattern-matching\/\"},\"wordCount\":876,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-pattern-matching\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"keywords\":[\"Constant pattern\",\"Logical pattern\",\"pattern matching\",\"property pattern\",\"Relational pattern\",\"Type patterns\",\"var pattern\"],\"articleSection\":[\"C#\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/csharp-pattern-matching\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/csharp-pattern-matching\/\",\"url\":\"https:\/\/code-maze.com\/csharp-pattern-matching\/\",\"name\":\"Pattern Matching in C# - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-pattern-matching\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-pattern-matching\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"datePublished\":\"2022-06-15T06:03:08+00:00\",\"description\":\"How to utilize pattern matching in C# using the following patterns: Type, Constant, Relational, Logical, Property, Positional, and Var pattern\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/csharp-pattern-matching\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/csharp-pattern-matching\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/csharp-pattern-matching\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"width\":1100,\"height\":620,\"caption\":\"C# Development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/csharp-pattern-matching\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Pattern Matching in C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\",\"name\":\"Code Maze\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"caption\":\"Code Maze\"},\"description\":\"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/company\/codemaze\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog\"],\"url\":\"https:\/\/code-maze.com\/author\/codemazecontributor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Pattern Matching in C# - Code Maze","description":"How to utilize pattern matching in C# using the following patterns: Type, Constant, Relational, Logical, Property, Positional, and Var pattern","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/csharp-pattern-matching\/","og_locale":"en_US","og_type":"article","og_title":"Pattern Matching in C# - Code Maze","og_description":"How to utilize pattern matching in C# using the following patterns: Type, Constant, Relational, Logical, Property, Positional, and Var pattern","og_url":"https:\/\/code-maze.com\/csharp-pattern-matching\/","og_site_name":"Code Maze","article_published_time":"2022-06-15T06:03:08+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","type":"image\/png"}],"author":"Code Maze","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Code Maze","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/csharp-pattern-matching\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/csharp-pattern-matching\/"},"author":{"name":"Code Maze","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04"},"headline":"Pattern Matching in C#","datePublished":"2022-06-15T06:03:08+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/csharp-pattern-matching\/"},"wordCount":876,"commentCount":0,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/csharp-pattern-matching\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","keywords":["Constant pattern","Logical pattern","pattern matching","property pattern","Relational pattern","Type patterns","var pattern"],"articleSection":["C#"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/csharp-pattern-matching\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/csharp-pattern-matching\/","url":"https:\/\/code-maze.com\/csharp-pattern-matching\/","name":"Pattern Matching in C# - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/csharp-pattern-matching\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/csharp-pattern-matching\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","datePublished":"2022-06-15T06:03:08+00:00","description":"How to utilize pattern matching in C# using the following patterns: Type, Constant, Relational, Logical, Property, Positional, and Var pattern","breadcrumb":{"@id":"https:\/\/code-maze.com\/csharp-pattern-matching\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/csharp-pattern-matching\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/csharp-pattern-matching\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","width":1100,"height":620,"caption":"C# Development"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/csharp-pattern-matching\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"Pattern Matching in C#"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04","name":"Code Maze","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","caption":"Code Maze"},"description":"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.","sameAs":["https:\/\/www.linkedin.com\/company\/codemaze\/","https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog"],"url":"https:\/\/code-maze.com\/author\/codemazecontributor\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/70272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=70272"}],"version-history":[{"count":3,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/70272\/revisions"}],"predecessor-version":[{"id":71111,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/70272\/revisions\/71111"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62189"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=70272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=70272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=70272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}