VB.NET Select…Case Statement Equivalent in C#

核能气质少年 提交于 2019-11-30 00:42:41

问题


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?


回答1:


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.




回答2:


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...




回答3:


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.




回答4:


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:

  • C# Switches vs. VB Case Statements
  • https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch


来源:https://stackoverflow.com/questions/2447065/vb-net-select-case-statement-equivalent-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!