Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two case statements in one switch statement

Tags:

c++

c#

switch(code)
{
    case 'A':
    case 'a':
         // to do 
    default:
         // to do 
}

Is there any way to merge the two "case" statements together?

like image 720
galaxyan Avatar asked Dec 20 '22 18:12

galaxyan


1 Answers

You just need break; for your current code like:

switch (code)
{
    case 'A':
    case 'a':
        break;
    // to do 
    default:
        // to do 
        break;
}

but if you are comparing for upper and lower case characters then you can use char.ToUpperInvariant and then specify cases for Upper case characters only:

switch (char.ToUpperInvariant(code))
{
    case 'A':
        break;
    // to do 
    default:
        // to do 
        break;
}
like image 165
Habib Avatar answered Dec 22 '22 08:12

Habib