学习札记五:C#中switch{case:...}与C中的不同 - Welcome to ...

来源:百度文库 编辑:神马文学网 时间:2024/04/27 17:04:12
学习札记五:C#中switch{case:...}与C中的不同
Posted on 2006-09-05 14:06希冀 阅读(84)评论(0)  编辑 收藏引用网摘 所属分类:C#学习札记
在C中,我们可以如下操作:
int x;
string str="hello";
……
switch (x)
{
case : 1
str = "my";
case : 2
str = str + " friend";
break;
default :
str = str + "everyone";
break;
}
……
而在C#中,如果如上运行,则会在str = "my"行报错,因为C#中,只有当case语句后不含内容时,才能紧跟下一个case,而不break.
如下:
switch (x)
{
case : 1
case : 2
str = "my";
str = str + " friend";
break;
default :
str = str + "everyone";
break;
}
可看上去这段程序却达不到上段程序的效果,但既然C#如此规定,我看一定有他的原因,这样的程序看上去不是规范易读些吗?