C#? : 操作员
有人可以向我解释以下编译器问题吗
错误:条件表达式的类型 无法确定,因为有 之间没有隐式转换 “字符串”和“整数”
// WORKS
string text = string.Format(
"the id is {0}", _Obj.Id.ToString());
// WORKS, without implicit conversion <<<
string text = string.Format(
"the id is {0}", _Obj.Id);
// WORKS
string text = string.Format(
"the id is {0}", (_Obj == null) ? "unknown" : _Obj.Id.ToString());
// NO WAY <<<
string text = string.Format(
"the id is {0}", (_Obj == null) ? "unknown" : _Obj.Id);
,也没有隐式转换。
Could somebody explain me the following compiler issue
Error: Type of conditional expression
cannot be determined because there is
no implicit conversion between
'string' and 'int'
// WORKS
string text = string.Format(
"the id is {0}", _Obj.Id.ToString());
// WORKS, without implicit conversion <<<
string text = string.Format(
"the id is {0}", _Obj.Id);
// WORKS
string text = string.Format(
"the id is {0}", (_Obj == null) ? "unknown" : _Obj.Id.ToString());
// NO WAY <<<
string text = string.Format(
"the id is {0}", (_Obj == null) ? "unknown" : _Obj.Id);
in the last example, there is no implicit conversion, as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
该问题与您使用
string.Format
无关。问题是这个表达式:编译器无法确定这个表达式的类型,因为
int
和string
之间没有隐式转换。您已经找到了解决方案 - 调用ToString
意味着表达式在任何情况下都会返回string
。修复它的另一种方法(但在这种情况下由于装箱而效率稍低)是明确告诉编译器如何执行转换。例如,您可以使用显式转换为object
:您的第二个示例无需显式转换即可工作,因为
string.Format
需要一个object
并且那里是从int
到object
的隐式转换。The problem is nothing to do with your usage of
string.Format
. The problem is this expression:The compiler cannot determine the type of this expression because there is no implicit conversion between
int
andstring
. You have already found the solution - callingToString
means that the expression returns astring
in either case. Another way you could have fixed it (but slightly less efficient in this case because of boxing) is to tell the compiler explicitly how to perform the conversion. For example, you can use an explicit cast toobject
:Your second example works without an explicit cast because
string.Format
expects anobject
and there is an implicit conversion fromint
toobject
.请参阅 Eric Lippert 博客文章强制转换运算符不遵守分配律。
See Eric Lippert's blog article Cast operators do not obey the distributive law.
这个表达式的计算结果是什么类型?
What is the type this expression evaluates to?
我认为你需要阅读 MSDN 上的这篇文章:
条件运算符。
特别是这部分:
I think you need to read this from MSDN:
Conditional Operator.
Specially this part:
编译器必须选择选择
string
类型或integer
(猜测),默认情况下不能反之亦然交换(无需隐式转换)string.Format 将
object
作为参数,因此没有问题将 Id(整数)转换为对象。compiler have to choose either pick
string
type orinteger
(guess) which is not can be exchanged vice versa by default (without implicit conversion)string.Format takes
object
as arg, so no problem to convert Id (integer) to object.最后一个有效,因为
string.format
将接受(string, object)
。第一个不起作用,因为
? :
运算符需要匹配类型。The last one works because
string.format
will accept(string, object)
.The first will not work because the
? :
operator needs matching types.问题是
和
不一样!
尝试将这个术语
视为
“哪个显然不起作用”。所以整个事情与
string.format
无关。the problem is that
and
are NOT the same!
Try to think the term
as
Which obviously is not working. So the whole thing has nothing to do with
string.format
.在第一种情况下(不起作用),如果
_Obj == null
,则返回一个string
,否则返回一个int
。这当然会导致问题,因为在这种情况下您会尝试将int
分配给string text
。in the first case (that does not work) if
_Obj == null
, you return astring
, else your return anint
. That of course causes a problem, since you in that case try to assign anint
tostring text
.