这是为什么code在C#中无效? [英] Why is this code invalid in C#?

查看:101
本文介绍了这是为什么code在C#中无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code将不能编译:

The following code will not compile:

string foo = "bar";
Object o = foo == null ? DBNull.Value : foo;

我得到:无法确定的条件前pression的错误1类型,因为有'System.DBNull'和'串'

要解决这个问题,我必须做这样的事情:

To fix this, I must do something like this:

string foo = "bar";
Object o = foo == null ? DBNull.Value : (Object)foo;

本投看起来毫无意义,因为这肯定是合法的:

This cast seems pointless as this is certainly legal:

string foo = "bar";
Object o = foo == null ? "gork" : foo;

在我看来,当三元分支是不同类型的,编译器将不autobox的值的类型的对象...但是,当它们是相同类型的则自动装箱是自动的。

It seems to me that when the ternary branches are of different types, the compiler will not autobox the values to the type object...but when they are of the same type then the autoboxing is automatic.

在我的脑海里第一个语句应该是合法的......

In my mind the first statement should be legal...

任何人都可以说明为什么编译器不允许这样,为什么C#的设计者选择这样做呢?我相信这是在Java中的法律......虽然我还没有证实这一点。

Can anyone describe why the compiler does not allow this and why the designers of C# chose to do this? I believe this is legal in Java...Though I have not verified this.

感谢。

编辑:我问为什么Java和C#手柄的理解这个区别,到底是怎么回事在C#,使这个无效的场景下。我知道如何使用三元,和我不是在寻找一个更好的方式code中的例子。我明白在C#中三元的规则,但我想知道为什么...

I am asking for an understanding of why Java and C# handle this differently, what is going on underneath the scenes in C# that make this invalid. I know how to use ternary, and am not looking for a "better way" to code the examples. I understand the rules of ternary in C#, but I want to know WHY...

修改(乔恩斯基特):删除了自动装箱的标签,因为没有拳击涉及这个问题。

EDIT (Jon Skeet): Removed "autoboxing" tag as no boxing is involved in this question.

推荐答案

编译器要求,无论是类型,第二个和第三个操作数是相同的,或者说,一个是隐式转换为其他的。你的情况,该类型的DBNull和字符串,这两者都不是隐式转换为其他。铸造其中任何对象解决了。

The compiler requires that either the types of second and third operands are the same, or that one is implicitly convertible to the other. In your case, the types are DBNull and string, neither of which is implicitly convertible to the other. Casting either of them to object solves that.

编辑:看起来它确实是Java的法律。它相当如何工作了,当涉及到方法重载做什么,我不知道......我只是看着JLS,它是什么条件的类型是非常不清楚时,有两个不兼容的参考类型有关。工作的C#的方式可能会更刺激偶然,但它更清晰海事组织。

Looks like it is indeed legal in Java. Quite how it works out what to do when it comes to method overloading, I'm not sure... I've just looked at the JLS, and it's extremely unclear about what the type of the conditional is when there are two incompatible reference types involved. The C# way of working may be more irritating occasionally, but it's clearer IMO.

在C#3.0规范中的相关章节是7.13,条件运算符:

The relevant section of the C# 3.0 spec is 7.13, the conditional operator:

的第二个和第三个操作数
  ?:操作员控制的类型
  有条件的前pression。设X和Y是
  类型的第二和第三的
  操作数。然后,

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,


      
  • 如果X和Y是相同的类型,则这是该条件
  • 的类型
      
  • 否则,如果一个隐式转换(第6.1节)根据X存在Y,
      但不能从Y到X,然后Y为
      类型条件前pression的。

  •   
  • 否则,如果一个隐式转换(第6.1节)选自Y存在X,
      但不从X到Y,则X是
      类型条件前pression的。

  •   
  • 否则,没有前pression类型可确定,编译时
      发生错误。

  •   

这篇关于这是为什么code在C#中无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆