【发布时间】:2012-04-25 16:17:41
【问题描述】:
从this question,我知道const string 可以是const 事物的串联。现在,枚举只是一组连续整数,不是吗?
那么为什么不能这样做:
const string blah = "blah " + MyEnum.Value1;
或者这个:
const string bloh = "bloh " + (int)MyEnum.Value1;
如何在 const 字符串中包含枚举值?
现实生活中的例子:在构建 SQL 查询时,我想要"where status <> " + StatusEnum.Discarded。
【问题讨论】:
-
猜测原因:连接整数可能涉及文化依赖性问题。您的
int.ToString()可能与我的int.ToString()不同。可能。只是一个猜测。尤其是负数,可能。 -
哦,对了,我没有仔细阅读我链接的问题。所以这不仅仅是枚举,实际上也不可能在 const 字符串中包含 const 整数。
-
是的;
const string foo = "abc" + 1;同样失败