我是一个 C # 新手,我只是遇到了一个问题。在处理三元运算符(? :
)时,C # 和 Java 是有区别的。
在下面的代码段中,为什么第4行不工作?编译器显示 there is no implicit conversion between 'int' and 'string'
的错误消息。第五行不好使。两个 List
都是对象,不是吗?
int two = 2;
double six = 6.0;
Write(two > six ? two : six); //param: double
Write(two > six ? two : "6"); //param: not object
Write(two > six ? new List<int>() : new List<string>()); //param: not object
然而,同样的代码在 Java 中也可以工作:
int two = 2;
double six = 6.0;
System.out.println(two > six ? two : six); //param: double
System.out.println(two > six ? two : "6"); //param: Object
System.out.println(two > six ? new ArrayList<Integer>()
: new ArrayList<String>()); //param: Object
C # 缺少哪些语言特性? 如果有,为什么没有添加?