C#? : 操作员

发布于 2024-10-02 02:50:20 字数 567 浏览 7 评论 0原文

有人可以向我解释以下编译器问题吗

错误:条件表达式的类型 无法确定,因为有 之间没有隐式转换 “字符串”和“整数”

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

明天过后 2024-10-09 02:50:20

该问题与您使用 string.Format 无关。问题是这个表达式:

(_Obj == null) ? "unknown" : _Obj.Id

编译器无法确定这个表达式的类型,因为 intstring 之间没有隐式转换。您已经找到了解决方案 - 调用 ToString 意味着表达式在任何情况下都会返回 string。修复它的另一种方法(但在这种情况下由于装箱而效率稍低)是明确告诉编译器如何执行转换。例如,您可以使用显式转换为 object

(_Obj == null) ? "unknown" : (object)_Obj.Id

您的第二个示例无需显式转换即可工作,因为 string.Format 需要一个 object 并且那里是从 intobject 的隐式转换。

The problem is nothing to do with your usage of string.Format. The problem is this expression:

(_Obj == null) ? "unknown" : _Obj.Id

The compiler cannot determine the type of this expression because there is no implicit conversion between int and string. You have already found the solution - calling ToString means that the expression returns a string 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 to object:

(_Obj == null) ? "unknown" : (object)_Obj.Id

Your second example works without an explicit cast because string.Format expects an object and there is an implicit conversion from int to object.

假情假意假温柔 2024-10-09 02:50:20

这个表达式的计算结果是什么类型?

(_Obj == null) ? "unknown" : _Obj.Id

What is the type this expression evaluates to?

(_Obj == null) ? "unknown" : _Obj.Id
苹果你个爱泡泡 2024-10-09 02:50:20

我认为你需要阅读 MSDN 上的这篇文章:
条件运算符

特别是这部分:

第二个和第三个操作数
?:操作员控制的类型
条件表达式。设 X 和 Y 为
第二个和第三个的类型
操作数。那么,

如果 X 和 Y 是同一类型,则
这是条件的类型
表达。否则,如果隐式
转换(第 6.1 节)存在于 X
到 Y,但不是从 Y 到 X,那么 Y 是
条件句的类型
表达。否则,如果隐式
转换(第 6.1 节)存在于 Y
到 X,但不是从 X 到 Y,则 X 是
条件句的类型
表达。否则无表情
可以确定类型,并且
发生编译时错误。

I think you need to read this from MSDN:
Conditional Operator.

Specially this part:

The second and third operands of the
?: operator control the type of the
conditional expression. Let X and Y be
the types of the second and third
operands. Then,

If X and Y are the same type, then
this is the type of the conditional
expression. Otherwise, if an implicit
conversion (Section 6.1) exists from X
to Y, but not from Y to X, then Y is
the type of the conditional
expression. Otherwise, if an implicit
conversion (Section 6.1) exists from Y
to X, but not from X to Y, then X is
the type of the conditional
expression. Otherwise, no expression
type can be determined, and a
compile-time error occurs.

乖乖 2024-10-09 02:50:20
"the id is {0}", (_Obj == null) ? "unknown" : _Obj.Id

编译器必须选择选择 string 类型或 integer (猜测),默认情况下不能反之亦然交换(无需隐式转换)

string text = string.Format("the id is {0}", _Obj.Id)

string.Formatobject 作为参数,因此没有问题将 Id(整数)转换为对象。

"the id is {0}", (_Obj == null) ? "unknown" : _Obj.Id

compiler have to choose either pick string type or integer (guess) which is not can be exchanged vice versa by default (without implicit conversion)

string text = string.Format("the id is {0}", _Obj.Id)

string.Format takes object as arg, so no problem to convert Id (integer) to object.

花间憩 2024-10-09 02:50:20

最后一个有效,因为 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.

﹏半生如梦愿梦如真 2024-10-09 02:50:20

问题是

// WORKS, without implicit conversion 
string text = string.Format( 
    "the id is {0}", _Obj.Id); 

// NO WAY 
string text = string.Format( 
    "the id is {0}", (_Obj == null) ? "unknown" : _Obj.Id); 

不一样!

尝试将这个术语

(_Obj == null) ? "unknown" : _Obj.Id);

视为

function int Eval(object obj)
{
  if (obj == null)
  {
    return "unknown";
  }
  else
  {
    return "1";
  }
}

“哪个显然不起作用”。所以整个事情与string.format无关。

the problem is that

// WORKS, without implicit conversion 
string text = string.Format( 
    "the id is {0}", _Obj.Id); 

and

// NO WAY 
string text = string.Format( 
    "the id is {0}", (_Obj == null) ? "unknown" : _Obj.Id); 

are NOT the same!

Try to think the term

(_Obj == null) ? "unknown" : _Obj.Id);

as

function int Eval(object obj)
{
  if (obj == null)
  {
    return "unknown";
  }
  else
  {
    return "1";
  }
}

Which obviously is not working. So the whole thing has nothing to do with string.format.

可可 2024-10-09 02:50:20

在第一种情况下(不起作用),如果_Obj == null,则返回一个string,否则返回一个int。这当然会导致问题,因为在这种情况下您会尝试将 int 分配给 string text

in the first case (that does not work) if _Obj == null, you return a string, else your return an int. That of course causes a problem, since you in that case try to assign an int to string text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文