简体   繁体   English

C# ? :运营商

[英]C# ? : operator

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' 错误:无法确定条件表达式的类型,因为'string'和'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. 在最后一个示例中,也没有隐式转换。

The problem is nothing to do with your usage of string.Format . 这个问题与你对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 . 编译器无法确定此表达式的类型,因为intstring之间没有隐式转换。 You have already found the solution - calling ToString means that the expression returns a string in either case. 您已经找到了解决方案 - 调用ToString意味着表达式在任何一种情况下都会返回一个string 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 : 例如,您可以使用显式强制转换来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 . 您的第二个示例在没有显式string.Format转换的情况下工作,因为string.Format需要一个object并且存在从intobject的隐式转换。

这个表达式的评估类型是什么?

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

I think you need to read this from MSDN: Conditional Operator . 我想你需要从MSDN中读取这个: 条件运算符

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. 设X和Y是第二个和第三个操作数的类型。 Then, 然后,

If X and Y are the same type, then this is the type of the conditional expression. 如果X和Y是相同的类型,那么这是条件表达式的类型。 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. 否则,如果从X到Y存在隐式转换(第6.1节),而不是从Y到X,则Y是条件表达式的类型。 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. 否则,如果从Y到X存在隐式转换(第6.1节),而不是从X到Y,则X是条件表达式的类型。 Otherwise, no expression type can be determined, and a compile-time error occurs. 否则,无法确定表达式类型,并发生编译时错误。

"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类型或integer (猜测),默认情况下不能相互交换(没有隐式转换)

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. string.Formatobject作为arg,因此将Id(整数)转换为对象没有问题。

The last one works because string.format will accept (string, object) . 最后一个有效,因为string.format将接受(string, object)

The first will not work because the ? : 第一个不会起作用,因为? : ? : operator needs matching types. ? :运算符需要匹配类型。

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 . 所以整件事与string.format无关。

in the first case (that does not work) if _Obj == null , you return a string , else your return an int . 在第一种情况下(不起作用)如果_Obj == null ,则返回一个string ,否则返回一个int That of course causes a problem, since you in that case try to assign an int to string text . 这当然会导致问题,因为在这种情况下,您尝试将int分配给string text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM