switch case到底是怎么工作的,我希望能够完全理解它

how does the switch case work exactly, I want to be able to understand it completely

我试图完全理解这段代码,我想我理解了其中的大部分内容,但有些部分我不太确定。很高兴能够清理一些东西。

    private void equals_Click(object sender, EventArgs e)
    {
        string[] hold = Sum.Text.Split(' ');//splits text with a space, not 100% sure this is correct
        switch (hold[1]) //not sure
        {
            case "+":// + is name of the case
                Result.Text = (Convert.ToDouble(hold[0]) + Convert.ToDouble(hold[2])).ToString(); 
                //displays in the result textbox > converts hold[0](first number) to double, 
                //hold[1] is the operation sign(+) > + sign to add the next number > convert hold[2] to double > converts it all to a string.

                break;//terminates the loop once the case has been selected.

你不明白的是什么?基本上它所做的是获取数组位置 1 的值并与您可能拥有的不同情况进行比较。因此,在这种情况下,如果 arr[1] 等于 '+' 则它进入这种情况,否则它会中断。你可以拥有多个 "case val:... break; " 通常是这样的:

switch(value)
{
  case "val1":
      code
      break;
  case "val2":
      code
      break;
  ...
  default:
      code
      return;
}