How To Handle Cases By Range in C# (and JavaScript)

How To Handle Cases By Range in C# (and JavaScript)

Sometimes you need to handle cases within a certain range, e.g., you have a customer and you need to apply different treatments depending on the salary value.

Although it's looks like a classic switch statement, Until C# 7.0 the case label must have been a static value, and you couldn’t use expression as a case ??


Before C# 7.0

With no support of the language, the primitive way to do it was by using if/else statements:

static string HandleCustomers(Customer customer)
{
   if (customer.salary >= 40_000)
   {
       return HandleRichCustomer(customer);
   }
 
   if (customer.salary >= 20_000 && customer.salary < 40_000) 
   { 
       return HandleWealthyCustomer(customer); 
   }

   if (customer.salary >= 10_000 && customer.salary < 20_000) 
   { 
       return HandleAverageCustomer(customer); 
   }

   if (customer.salary >= 00_000 && customer.salary < 10_000) 
   { 
       return HandlePoorCustomer(customer); 
   }

   throw new IllegalSalaryException();

}

As you can see, the code is very long and get even longer with more logic... 


C# 7.0

C# 7.0 added two new features to the switch statement: 'Type pattern' and 'When clause'.

Type pattern:

The Type pattern added the ability to perform pattern matching by type, and apply a conversion type to the object:

static string TypePattern<T>(T customer)
{
    switch (customer)
    {
        case RichCustomer richCustomer: 
            return $"RichCustomer salary: {richCustomer.salary}";

        case WealthyCustomer wealthyCustomer: 
            return $"WealthyCustomer salary: {wealthyCustomer.salary}";

        case AverageCustomer averageCustomer: 
            return $"AverageCustomer salary: {averageCustomer.salary}";

        case PoorCustomer poorCustomer: 
            return $"RichCustomer salary: {poorCustomer.salary}";

        default:
            throw new IllegalSalaryException() 
    }
}

When clause:

The when clause is an additional condition which you can add to the case, using the when label:

static string WhenClause<T>(T Customer)
{
   switch (Customer)
   {
       case Customer richCustomer 
       when richCustomer.salary >= 40_000: 
           return HandleRichCustomer(richCustomer);

       case Customer wealthyCustomer 
       when wealthyCustomer.salary < 40_000 && wealthyCustomer.salary >= 20_000:
           return HandleWealthyCustomer(wealthyCustomer);

       case Customer avaregeCustomer 
       when avaregeCustomer.salary < 20_000 && avaregeCustomer.salary >= 10_000:
           return HandleAverageCustomer(avaregeCustomer);
 
       case Customer poorCustomer 
       when poorCustomer.salary < 10_000 && poorCustomer.salary >= 0:
           return HandlePoorCustomer(poorCustomer);

       default: throw new IllegalSalaryException();
   }
}

 

C# 8.0

C# 8.0 came and introduced a new feature: 'switch expression'. Instead of cases you now have multiple expressions, which are separated by a comma, The first match of the pattern on the right, returns the expression on the left.

Customer c => "I am a customer"

(You can read here about more new patterns, as: "positional pattern", "Property Pattern" and "Tuple pattern")

With the 'switch expression' you can write the same code, shorter:

static string HandleCustomers(Customer customer) => customer.salary switch
{ 
   int salary when salary >= 40_000 => HandleRichCustomer(customer),
   int salary when salary >= 20_000 && salary < 40_000 => HandleWealthyCustome r(customer),
   int salary when salary >= 10_000 && salary < 20_000 => HandleAverageCustomer(customer),
   int salary when salary >= 00_000 && salary < 10_000 => HandlePoorCustomer(customer),
   _ => throw new IllegalSalaryException()
});

 (Notice the Discard Pattern ('_') that match any expression)


C# 9.0

The latest version of C#, came with some more new features:

New Logical Patterns: 

C# 9.0 added (finally! ??) new logical operators: 'and', 'or' and 'not'.

Relational Pattern:

The Relational Pattern lets you compare constant expression (int, enum, char or float) with relational operator ('<’, ‘>', '<=', '>=').

So, in C# 9.0, our 'HandleCustomer' function can be written like this:

static string HandleCustomers(Customer customer) => customer.salary switch
{
    >= 40_000 => HandleRichCustomer(customer),
    >= 20_000 and < 40_000 => HandleWealthyCustomer(customer),
    >= 10_000 and < 20_000 => HandleAverageCustomer(customer),
    >= 00_000 and < 10_000 => HandlePoorCustomer(customer),
    _ => throw new IllegalSalaryException()
};

Short and clean ??

(You can read here all the patterns in the C# language).


 JavaScript

As of today, despite the significant growth of the language, JavaScript doesn't yet support all of the new C# features.

But you can still achieve the same result by using switch(true).

Because JavaScript allows the case label to be an expression, you can do a switch statement on the 'true' key word and it will enter the first case expression that returns true:

No alt text provided for this image

(I added a picture because for some reason, the linkedin atricle got 'broken' with the upper code ???♂?)


Thanks,

Ziv Ben-Or

[email protected]

+972545882011

要查看或添加评论,请登录

Ziv Ben-Or的更多文章

  • Different ways to return data in C#

    Different ways to return data in C#

    When we write a function with complex logic or when the function calls for a third party resource - aside from the…

    1 条评论
  • 3 Questions for you to see how well do you know JavaScript

    3 Questions for you to see how well do you know JavaScript

    Here are 3 question to examine your JavaScript knowledge. Please try to answer before you run and execute the code.

    3 条评论
  • Microsoft Dynamics CRM 365 - 3 JavaScript Bugs

    Microsoft Dynamics CRM 365 - 3 JavaScript Bugs

    We are working with Dynamics 365 V 8.2, and in the last year we found 3 bugs when trying to insert data into a record…

    1 条评论
  • Debug Web Resources with Fiddler

    Debug Web Resources with Fiddler

    Today I want to share with you a much easier and faster way to debug your Web-Resources during your development - with…

    3 条评论
  • Recently viewed items Performance issue/bug.

    Recently viewed items Performance issue/bug.

    Today I want to share with you one of the Microsoft's Dynamics CRM issues that causes a serious performance issue…

    12 条评论
  • CRM Visual Studio Extensions part 3

    CRM Visual Studio Extensions part 3

    Today I want to introduce you to Marat Deykun's extension – Microsoft Dynamics CRM Web Resources Updater (yes, a long…

    3 条评论
  • CRM Visual Studio Extensions part 2

    CRM Visual Studio Extensions part 2

    Today I want to introduce you to another Mads Kristensen extension – Web Compiler. This article is for those who…

  • CRM Visual Studio Extensions part 1

    CRM Visual Studio Extensions part 1

    I want to share with you the next series of articles with recommended Visual Studio extensions for Microsoft Dynamics…

  • C# - Using the Result Concept.

    C# - Using the Result Concept.

    I want to recommend you a great concept I'm working with – Result. A colleague of my, Tal Aitelberg, introduced me this…

    2 条评论
  • Managing configuration with multiple environments in Visual Studio - the right way!

    Managing configuration with multiple environments in Visual Studio - the right way!

    It's very common we develop a web application using App.config/Web.

    2 条评论

社区洞察

其他会员也浏览了