Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#'s switch statement is case-sensitive. Is there a way to toggle it so it becomes case-insensitive? [duplicate]

C#'s switch() statement is case-sensitive. Is there a way to toggle it so it becomes case-insensitive?

==============================

Thanks, But , I don't like these solutions;

Because case conditions will be a variable , and I don't know if they ALL are UPPER or lower.

like image 465
xiemails Avatar asked Nov 27 '22 18:11

xiemails


2 Answers

Yes - use ToLower() or ToLowerInvariant() on its operands. For example:

switch(month.ToLower()) {
    case "jan":
    case "january": // These all have to be in lowercase
         // Do something
         break;
}
like image 121
Ry- Avatar answered Dec 08 '22 00:12

Ry-


You can do something like this

switch(yourStringVariable.ToUpper()){
    case "YOUR_CASE_COND_1":
     // Do your Case1
    break;

    case "YOUR_CASE_COND_2":
    // Do your Case 2
    break;

    default:
}
like image 42
YetAnotherUser Avatar answered Dec 08 '22 00:12

YetAnotherUser