{"id":7534,"date":"2025-02-09T15:07:31","date_gmt":"2025-02-09T15:07:31","guid":{"rendered":"http:\/\/localhost:8080\/csharp-test\/docs\/basic\/conditional-statements\/switch-expression-cezd_hash-8-0ezd_plus\/"},"modified":"2025-09-10T05:23:07","modified_gmt":"2025-09-10T05:23:07","slug":"switch-expression","status":"publish","type":"docs","link":"https:\/\/www.completecsharptutorial.com\/docs\/csharp\/conditional-statements\/switch-expression\/","title":{"rendered":"Switch Expression (C# 8.0+)"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"7534\" class=\"elementor elementor-7534\">\n\t\t\t\t<div class=\"elementor-element elementor-element-679aadf e-flex e-con-boxed e-con e-parent\" data-id=\"679aadf\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-c045634 elementor-widget elementor-widget-heading\" data-id=\"c045634\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h1 class=\"elementor-heading-title elementor-size-default\">C# Switch Expression \u2013 A Shorter, Cleaner Way!<\/h1>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-46322dc e-flex e-con-boxed e-con e-parent\" data-id=\"46322dc\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-52603fa elementor-widget elementor-widget-text-editor\" data-id=\"52603fa\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Hey there, C# learner! \ud83d\udc4b Ever felt <strong>frustrated<\/strong> writing long switch statements with all those <code>case<\/code> and <code>break<\/code> keywords? \ud83d\ude35 Traditional switch cases work fine, but they <strong>take up too much space<\/strong> and look repetitive.<\/p><p>Good news! <strong>C# 8.0<\/strong> introduced the <strong>switch Expression<\/strong> \u2013 a <strong>shorter and cleaner<\/strong> way to write switch cases. \ud83c\udf89 No more endless <code>break<\/code> statements!<\/p><h3><strong>Why Use Switch Expression? \ud83e\udd14<\/strong><\/h3><ol class=\"ordered-list\"><li><strong>Less code, more readability<\/strong> \u2013 No need for <code>case<\/code> and <code>break<\/code>.<\/li><li><strong>Faster execution<\/strong> \u2013 It\u2019s optimized for better performance.<\/li><li><strong>More fun to write!<\/strong> \u2013 Who doesn\u2019t love <strong>cleaner code?<\/strong> \ud83d\ude0e<\/li><\/ol>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-a6be379 elementor-widget elementor-widget-text-editor\" data-id=\"a6be379\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3>Syntax of Switch Expression in C#<\/h3>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-7d8bb54 e-flex e-con-boxed e-con e-parent\" data-id=\"7d8bb54\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-cad40d8 elementor-widget elementor-widget-text-editor\" data-id=\"cad40d8\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Alright, buddy! Before we jump into more examples, let\u2019s <strong>crack the syntax<\/strong> of the <strong>switch Expression in C#<\/strong>. Don&#8217;t worry\u2014it&#8217;s super simple! \ud83d\ude0e<\/p><p>Here\u2019s what it looks like:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-1eba975 e-flex e-con-boxed e-con e-parent\" data-id=\"1eba975\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-100c278 elementor-widget elementor-widget-docly_code_syntax_highlighter\" data-id=\"100c278\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"docly_code_syntax_highlighter.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t                    <div class=\"docly-source-code prism\" data-lng-type=\"csharp\">\r\n\t\t\t<pre>\r\n\t\t\t\t<code class=\"language-csharp\">\r\n\t\t\t\t\tvariable = input switch\r\n{\r\n    case1 =&gt; result1,\r\n    case2 =&gt; result2,\r\n    case3 =&gt; result3,\r\n    _ =&gt; defaultResult \/\/ Default case (optional)\r\n};\r\n\t\t\t\t<\/code>\r\n\t\t\t<\/pre>\r\n            <\/div>\r\n                \t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-8344453 e-flex e-con-boxed e-con e-parent\" data-id=\"8344453\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-5f7e78e elementor-widget elementor-widget-text-editor\" data-id=\"5f7e78e\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h4><strong>Breaking it Down Like a Pro \ud83d\udd0d<\/strong><\/h4><ol class=\"ordered-list steps\"><li><strong>No <code>case<\/code> or <code>break<\/code> needed<\/strong> \u2013 Just write <code>caseValue =&gt; result<\/code>.<\/li><li><strong>Each case is separated by a comma (<code>,<\/code>)<\/strong> \u2013 No <code>{}<\/code> or <code>:<\/code> needed!<\/li><li><strong>The <code>_<\/code> (underscore) means &#8220;default case&#8221;<\/strong> \u2013 It handles all unmatched cases.<\/li><\/ol><p>Now, let\u2019s turn this into a <strong>real-life example<\/strong>!<\/p><p><strong>Example: Convert Numbers to Words<\/strong><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-4fc9881 e-flex e-con-boxed e-con e-parent\" data-id=\"4fc9881\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-5d47201 elementor-widget elementor-widget-docly_code_syntax_highlighter\" data-id=\"5d47201\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"docly_code_syntax_highlighter.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t                    <div class=\"docly-source-code prism\" data-lng-type=\"csharp\">\r\n\t\t\t<pre>\r\n\t\t\t\t<code class=\"language-csharp\">\r\n\t\t\t\t\tint number = 3;\r\n\r\nstring word = number switch\r\n{\r\n    1 =&gt; &quot;One&quot;,\r\n    2 =&gt; &quot;Two&quot;,\r\n    3 =&gt; &quot;Three&quot;,\r\n    _ =&gt; &quot;Unknown&quot;\r\n};\r\n\r\nConsole.WriteLine(word);\r\n\t\t\t\t<\/code>\r\n\t\t\t<\/pre>\r\n            <\/div>\r\n                \t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-4b49a42 e-flex e-con-boxed e-con e-parent\" data-id=\"4b49a42\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-6a5c6c1 elementor-widget elementor-widget-text-editor\" data-id=\"6a5c6c1\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Output (if <code>number = 3<\/code>):<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-d87771f elementor-widget elementor-widget-docly_code_syntax_highlighter\" data-id=\"d87771f\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"docly_code_syntax_highlighter.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t                    <div class=\"docly-source-code prism-dark\" data-lng-type=\"markup\">\r\n\t\t\t<pre>\r\n\t\t\t\t<code class=\"language-markup\">\r\n\t\t\t\t\tThree\r\n\t\t\t\t<\/code>\r\n\t\t\t<\/pre>\r\n            <\/div>\r\n                \t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-4f6f01b elementor-widget elementor-widget-text-editor\" data-id=\"4f6f01b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Pretty cool, right? \ud83d\ude03 <strong>No <code>case<\/code>, no <code>break<\/code>, just clean code!<\/strong><\/p><h4><strong>Why is Switch Expression Better?<\/strong><\/h4><p class=\"alert\">\u2705 <strong>Less typing<\/strong> \u2013 Saves time and effort!<br \/>\u2705 <strong>Easier to read<\/strong> \u2013 No <code>case<\/code> blocks cluttering your code.<br \/>\u2705 <strong>No missing <code>break<\/code> errors<\/strong> \u2013 One less thing to worry about!<\/p><p class=\"alert\">Now that you know the <strong>syntax<\/strong>, go ahead and try it in your own programs! \ud83d\ude80<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-7dfa6b8 e-flex e-con-boxed e-con e-parent\" data-id=\"7dfa6b8\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-f07fb6f elementor-widget elementor-widget-text-editor\" data-id=\"f07fb6f\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3><strong>Real-World Example: Order Processing System<\/strong><\/h3><p>Imagine you are building an <strong>e-commerce website<\/strong>. Users order different items, and each item has a <strong>delivery time<\/strong>:<\/p><ul class=\"ordered-list steps\"><li><strong>Electronics<\/strong> take <strong>7 days<\/strong> \ud83d\udcf1<\/li><li><strong>Clothing<\/strong> takes <strong>3 days<\/strong> \ud83d\udc55<\/li><li><strong>Books<\/strong> take <strong>2 days<\/strong> \ud83d\udcda<\/li><li><strong>Other items<\/strong> take <strong>5 days<\/strong> \ud83d\udce6<\/li><\/ul><p>Instead of writing a <strong>long switch statement<\/strong>, let\u2019s <strong>simplify<\/strong> it with a <strong>switch Expression in C#<\/strong>!<\/p><h4><strong>C# Switch Expression Example \ud83d\udccc<\/strong><\/h4>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-e741427 e-flex e-con-boxed e-con e-parent\" data-id=\"e741427\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-5d8d712 elementor-widget elementor-widget-docly_code_syntax_highlighter\" data-id=\"5d8d712\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"docly_code_syntax_highlighter.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t                    <div class=\"docly-source-code prism\" data-lng-type=\"csharp\">\r\n\t\t\t<pre>\r\n\t\t\t\t<code class=\"language-csharp\">\r\n\t\t\t\t\tusing System;\r\n\r\nclass Program\r\n{\r\n    static void Main()\r\n    {\r\n        string itemCategory = &quot;Electronics&quot;; \/\/ Try changing this to &quot;Books&quot; or &quot;Clothing&quot;\r\n\r\n        int deliveryDays = itemCategory switch\r\n        {\r\n            &quot;Electronics&quot; =&gt; 7,\r\n            &quot;Clothing&quot; =&gt; 3,\r\n            &quot;Books&quot; =&gt; 2,\r\n            _ =&gt; 5 \/\/ Default case for other items\r\n        };\r\n\r\n        Console.WriteLine($&quot;Your {itemCategory} will be delivered in {deliveryDays} days.&quot;);\r\n    }\r\n}\r\n\t\t\t\t<\/code>\r\n\t\t\t<\/pre>\r\n            <\/div>\r\n                \t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-db28136 elementor-widget elementor-widget-text-editor\" data-id=\"db28136\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Output (if <code>itemCategory = \"Electronics\"<\/code>):<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-224f424 elementor-widget elementor-widget-docly_code_syntax_highlighter\" data-id=\"224f424\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"docly_code_syntax_highlighter.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t                    <div class=\"docly-source-code prism-dark\" data-lng-type=\"markup\">\r\n\t\t\t<pre>\r\n\t\t\t\t<code class=\"language-markup\">\r\n\t\t\t\t\tYour Electronics will be delivered in 7 days.\r\n\t\t\t\t<\/code>\r\n\t\t\t<\/pre>\r\n            <\/div>\r\n                \t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-4eb5ec4 elementor-widget elementor-widget-text-editor\" data-id=\"4eb5ec4\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h4><strong>How Does This Code Work? \ud83e\uddd0<\/strong><\/h4><ol class=\"ordered-list steps\"><li>The user selects an <strong>item category<\/strong> (e.g., <code>\"Electronics\"<\/code>).<\/li><li>The <strong>switch Expression<\/strong> checks the category and <strong>returns the delivery time<\/strong>.<\/li><li>No need for <code>case<\/code>, <code>break<\/code>, or <code>return<\/code>. Just <strong>clean, simple code!<\/strong><\/li><li>If the category doesn\u2019t match, the <strong>default case (<code>_<\/code>)<\/strong> returns <code>5<\/code> days.<\/li><\/ol><p>See how much <strong>cleaner<\/strong> this is compared to a <strong>traditional switch<\/strong>? \ud83d\ude80<\/p><h3><strong>Conclusion \ud83c\udfaf<\/strong><\/h3><p>The <strong>switch Expression in C#<\/strong> is a <strong>game-changer<\/strong> for writing <strong>short, clean, and readable<\/strong> code. Instead of using <strong>traditional switch statements<\/strong>, you can <strong>save time<\/strong> and <strong>avoid unnecessary lines of code<\/strong>.<\/p><p>Next time you write a switch case, ask yourself: <strong>Can I use a switch Expression instead?<\/strong> If yes, go for it! \ud83d\ude0e<\/p><h3><strong>Next What? \ud83d\ude80<\/strong><\/h3><p>You\u2019re doing great! \ud83d\udd25 But there\u2019s <strong>more awesomeness ahead<\/strong>.<\/p><p>In the <strong>next lesson<\/strong>, we\u2019ll explore <strong>Pattern Matching<\/strong> \u2013 a powerful way to make your <code>switch<\/code> statements even <strong>smarter!<\/strong> \ud83e\udde0<\/p><p>\ud83d\udc49 <strong>Excited? Let\u2019s dive into Pattern Matching next! \ud83d\ude80<\/strong><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>C# Switch Expression \u2013 A Shorter, Cleaner Way! Hey there, C# learner! \ud83d\udc4b Ever felt frustrated writing long switch statements with all those case and break keywords? \ud83d\ude35 Traditional switch cases work fine, but they take up too much space and look repetitive. Good news! C# 8.0 introduced the switch Expression \u2013 a shorter and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":6031,"menu_order":63,"comment_status":"open","ping_status":"closed","template":"","doc_tag":[],"class_list":["post-7534","docs","type-docs","status-publish","hentry","no-post-thumbnail"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.completecsharptutorial.com\/wp-json\/wp\/v2\/docs\/7534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.completecsharptutorial.com\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/www.completecsharptutorial.com\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/www.completecsharptutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.completecsharptutorial.com\/wp-json\/wp\/v2\/comments?post=7534"}],"version-history":[{"count":5,"href":"https:\/\/www.completecsharptutorial.com\/wp-json\/wp\/v2\/docs\/7534\/revisions"}],"predecessor-version":[{"id":10083,"href":"https:\/\/www.completecsharptutorial.com\/wp-json\/wp\/v2\/docs\/7534\/revisions\/10083"}],"up":[{"embeddable":true,"href":"https:\/\/www.completecsharptutorial.com\/wp-json\/wp\/v2\/docs\/6031"}],"wp:attachment":[{"href":"https:\/\/www.completecsharptutorial.com\/wp-json\/wp\/v2\/media?parent=7534"}],"wp:term":[{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/www.completecsharptutorial.com\/wp-json\/wp\/v2\/doc_tag?post=7534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}