C/C++/C#是否支持基于string的switch-case??

博客主要对比了C/C++和C#中switch语句支持的类型。C/C++支持int和char,case只能是整型、字符或枚举常量;C#支持int、char、string。还给出了相关MSDN参考内容及优快云帖子链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C/C++只支持整型值(用字符常量也可以,但编译器事实上也是把字符常量量转化成了整型值的).

其实:

C/C++支持 int 和char,switch语句中的case只能是常量.是整型,字符或枚举三种.
C#支持int、char、string。

Enum其实也就是int……

From MSDN:

In ANSI C, the expressions that define the value of an enumerator constant always have int type; thus, the storage associated with an enumeration variable is the storage required for a single int value. An enumeration constant or a value of enumerated type can be used anywhere the C language permits an integer expression.

____________________________________________________________

From MSDN:

switch (expression)
{
   case constant-expression:
      statement
      jump-statement
   [default:
      statement
      jump-statement]
}

C++
The expression must be of an integral type or of a class type for which there is an unambiguous conversion to integral type.
(参考地址:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_The_C.2b2b_.switch_Statement.asp

C#
expression
An integral or string type expression.
(参考地址:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfTheSwitchStatement.asp

___________________________________________________________________

参见优快云 的帖子:

http://community.youkuaiyun.com/Expert/topic/3857/3857598.xml?temp=.1475489

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值