C#的switch语句

it2022-05-09  27

 使用switch语句时需注意以下几点:

      不准遍历

和C与C++的不同之处

例子:

total=365; switch(month){   case 1:total-=31;   case 2:total-=28;   case 3:total-=31;   case 4:total-=30;   case 5:total-=31;   case 6:total-=30;   case 7:total-=31;   case 8:total-=31;       case 9:total-=30;       case 10:total-=31;       case 11:total-=30;   case 12:total-=31;       default:total+=day; } 这在C#会出错,在C#中,要求  case  与   default  之后都有break或,goto 不须重复它继续遍历 为的是防止很多case漏写了break什么的 goto的用法:

●goto case label:跳至标签语句执行

●goto default:跳至default标签执行

那样,我们将上面的程序可以改写为:

total 365; switch(month){   case 1:total-=31;goto case 2;   case 2:total-=28;goto case 3;   case 3:total-=30;goto case 4;   case 4:total-=31;goto case 5;   case 5:total-=30;goto case 6;   case 6:total-=31;goto case 7;   case 7:total-=30;goto case 8;   case 8:total-=31;goto case 9;   case 9:total-=30;goto case 10;   case 10:total-=31;goto case 11;   case 11:total-=30;goto case 12;   case 12:total-=31;goto default;   default:total+=day; } 在避免了c,c++中常出现的由于漏写break而造成的错误的同时,“不准遍历”的原则还使得我们可以任意排列switch语句中的case项而不会影响switch语句的功能。本文发表于http://bianceng.cn

另外,一般来说每个switch项都以break,goto case或goto default结束,但实际上任何一种不导致“遍历”的结构都是允许的,例如throw和return语句同样可以跳出控制之外,因而下例是正确的:

switch(i){ case 0:   while(true) F(); case 1:   throw new ArgumentException(); case 2:   return; } 把字符串当成常量表达式

VB程序员可能已经习惯把字符串当成常量表达式来使用,但C和C++却不支持这一点。但是,C#的switch语句与c,c++的另一个不同点是,C#可以把字符串当成常量表达式来使用。所以switch语句的控制类型可以是string类型。

下面的例子是实现浮动窗口提示。在Windows操作系统中,我们把鼠标移到某一个控件上停留几秒钟,将会出现一个浮动提示,说明该控件的作用等。例子中的GetButtonCaption方法用于获得按钮上的文字,ShowMessage表示在浮动提示窗口中显示信息。

string text=GetButtonCaption(); switch(text) {   case "OK":ShowMessage("save the change and exit");break;   case "Retry":ShowMessage("return and retry");break;   case "Abort":ShowMessage("Abort the change and exit");break;   case "Help":ShowMessage("Get help from system");break; } 实际上,在老版本的C语言中,switch语句的控制类型只允许是整数表达式或字符型表达式,而ANSI标准放宽了这一要求。C#中是对switch语句的控制类型的又一扩展。而且在C#中case标签的引用值允许是null。

转载于:https://www.cnblogs.com/ijunxiong/articles/1122122.html


最新回复(0)