代码之家  ›  专栏  ›  技术社区  ›  serhio

C?操作员

  •  4
  • serhio  · 技术社区  · 14 年前

    有人能给我解释一下下面的编译器问题吗

    错误:条件表达式的类型 无法确定,因为 之间没有隐式转换 '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);
    

    在上一个示例中,也没有隐式转换。

    8 回复  |  直到 14 年前
        2
  •  10
  •   Mark Byers    14 年前

    string.Format

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

    int string ToString object

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

    int 对象 .

        3
  •  2
  •   György Andrasek    14 年前

    此表达式计算的类型是什么?

    (_Obj == null) ? "unknown" : _Obj.Id
    
        4
  •  1
  •   serhio    14 年前
        5
  •  0
  •   Nick Martyshchenko    14 年前
    "the id is {0}", (_Obj == null) ? "unknown" : _Obj.Id
    

    string integer

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

    string.Format object

        6
  •  0
  •   UpTheCreek    14 年前

    string.format (string, object)

    ? :

        7
  •  0
  •   Yves M.    14 年前

    // 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

        8
  •  -1
  •   Øyvind Bråthen    14 年前

    _Obj == null string int string text