switch语句使用总结
5077 点击·0 回帖
![]() | ![]() | |
![]() | switch语句 ? 语法 ? 用于整数类类型 ? case后的标志必须是编译时为常数 ? 没有表示范围的缩略形式 string DaySuffix(int days) { string result = "th"; if (days / 10 != 1) switch (days % 10) { case 1 : result = "st"; break; case 2 : result = "nd"; break; case 3 : result = "rd"; break; default: //表示不符合上面条件的情况 result = "th"; break; } return result; } 你只能对整型、字符串或可以隐式转换为整型或字符串的用户自定义类型使用switch语句。case标志必须在编译时是常数。 C#中没有Visual Basic中的Is关键字在case中进行比较,例如: switch (expression()) { case Is < 42 : //错误 www.atcpu.com case method() : //错误 } C#中没有范围比较符。 switch (expression()) { case 16 To 21 : //错误 case 16..21 : //错误 } 注意:每个case段必须包括break语句,default语句也不例外。 | |
![]() | ![]() |