VB.NET Select…Case Statement Equivalent in C#

2019-02-11 16:38发布

I just started using C# and I've got a couple of issues. Is there any way to code the C# equivalent of the VB.NET Select statement like the following?

Select Object.Name.ToString()
    Case "Name1"
        'Do something
    Case "Name2"
        'Do something else
    Case Else
        'Do the default action
End Select

Any help would be greatly appreciated.


Thanks for the input so far now what about if I hook several controls to one event handler as in the following and I want to perform a slightly different action for each control:

Private Sub Button_Click(sender as Object, e as EventArgs) _
  Handles button1.Click, Button2.Click

    'do a general activity

    Select CType(sender, Button).Name
        Case button1.Name
            'do something
        Case button2.Name
            'do something else
        Case Else
            'do the defalut action
    End Select
End Sub

Is there any way of doing the above select statement in C# without having to use nested ifs?

4条回答
戒情不戒烟
2楼-- · 2019-02-11 16:55

With C# 7, switch has been significantly enhanced, and it's now possible to apply more conditions within cases, although it's still not as "clean" as the VB version. E.g. you could do something like:

switch (examScore)
{
  case int es when es >= 90: grade = "A"; break;
  case int es when es >= 80: grade = "B"; break;
  case int es when es >= 70: grade = "C"; break;
  case int es when es >= 60; grade = "D"; break;
  default: grade = "F"; break;
}

Taken from / References:

查看更多
混吃等死
3楼-- · 2019-02-11 17:04

You'd be looking for the switch statement...

switch (Object.Name.ToString())
{
   case  "Name1":
     //Do something
     break;
   default:
     //default
     break;
}

Note that the breaks are import, otherwise the program will drop through your cases. You should be able to find this on almost any C# introduction...

查看更多
爷、活的狠高调
4楼-- · 2019-02-11 17:05

Use a switch statement.

switch (object.Name.ToString()) {
  case "Name1":
    break;
    //Do something 
  case "Name2":
    break;
    //Do something else 
  default:
    break;
   //Do the default action 
}

And don't forget that there is a free online conversion tool that allows you to convert VB.net to C# and viceversa.

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-02-11 17:11

I have come to find over time that some VB.NET Select...Case constructs do not apply in C# and the only way around is to write a lot of ifs.

For instance, in VB.NET, you can write:

Dim num as Integer = 5

Select Case num
    Case 1 to 10
        'do something

    Case 11 to 20
        'do another thing

    Case Else
        'do the default
End Select

But there is no switch construct in C# that allows you to do something of this sort. You'll have to code in roundabout like so:

int num = 5;

switch (num)
{
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    case 10:
        //do something
        break;
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    case 16:
    case 17:
    case 18:
    case 19:
    case 20:
        //do something else
        break;
    default:
        //do the default
        break;
}

Or if you happen to be working with Double or any type which is made up of continuous instead of discrete values, you'll have to use ifs to get the required action.

Reason? C#'s switch requires constants for the various cases. This is different from VB.NET's Select Case which allows writing ranges.

查看更多
登录 后发表回答