none
switch only for C# 8.0 or upper? RRS feed

  • Question

  • Hi 

    public static decimal ComputeSalesTax(Address location, decimal salePrice) =>
        location switch
        {
            { State: "WA" } => salePrice * 0.06M,
            { State: "MN" } => salePrice * 0.75M,
            { State: "MI" } => salePrice * 0.05M,
            // other cases removed for brevity...
            _ => 0M
        };

    C# 7.3 has that?


    Life is not so easy.......

    Thursday, October 31, 2019 2:09 AM

Answers

  • In my view, it's better to set up a Dictionary<string,decimal> and initialize that once at start up time.  That way, the lookup becomes a single line that is much more readable.

    Minnesota does not have a 75% state sales tax...  ;)


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by QLoveQ Monday, November 4, 2019 3:54 AM
    Sunday, November 3, 2019 4:43 AM

All replies

  • The switch 'expression' as you have used it is new in version 8 of c#. The switch 'statement' is in earlier versions.

    https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#switch-expressions

    Thursday, October 31, 2019 3:12 AM
  • Hi QLoveQ, 

    Thank you for posting here.

    Switch statement has been updated in C# 8, so you need to update the C# version to C# 8.0 in order to use the latest switch statement.

    You can refer to the following reference to install C# 8.0.

    Install and Use C# 8 in Visual Studio 2019

    Here’s a reference about how to use switch Expressions In C# 8.

    Switch Expressions In C# 8

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, October 31, 2019 6:09 AM
    Moderator
  • C# 8 only for the switch expression. The switch statement, including the beginnings of pattern matching, are supported in 7.3. However C# 8 is only supported with .NET Standard 2.1. So if you're using .NET Framework you cannot use the C# 8 features as it does not support .NET Standard 2.1. Most of the new language features require runtime support and, at this time, .NET Framework isn't getting them. We'll see when .NET 5 comes around but until then C# 8 is not an option for .NET Framework apps.

    Personally I don't find the switch expression remotely useful or readable. Your code can just as easily be written with the switch statement and accomplish the same thing. And it works on 7.3.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, October 31, 2019 1:41 PM
    Moderator
  • In my view, it's better to set up a Dictionary<string,decimal> and initialize that once at start up time.  That way, the lookup becomes a single line that is much more readable.

    Minnesota does not have a 75% state sales tax...  ;)


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by QLoveQ Monday, November 4, 2019 3:54 AM
    Sunday, November 3, 2019 4:43 AM
  • In my view, it's better to set up a Dictionary<string,decimal> and initialize that once at start up time.  That way, the lookup becomes a single line that is much more readable.

    Minnesota does not have a 75% state sales tax...  ;)


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Yes, Agree.

    Life is not so easy.......

    Monday, November 4, 2019 3:54 AM