单行if语句生成强制转换错误 [英] Single line if statement generates cast error

查看:225
本文介绍了单行if语句生成强制转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#的新手,所以我遇到了一个简短陈述的问题。我想转换...

new to C# so I'm having an issue with a short-hand statement. I want to convert...

if (m_dtLastLogin == null)
    drow["LastLogin"] = DBNull.Value;
else
    drow["LastLogin"] = m_dtLastLogin;

drow["LastLogin"] = (m_dtLastLogin == null) ? System.DBNull.Value : m_dtLastLogin;

长版本效果很好,但是,短版本会生成错误Type of因为System.DBNull和System.DateTime?之间没有隐式转换,所以无法确定条件。我的支持代码基本上是......

The long-hand version works great, however, the short-hand version generates the error "Type of conditional cannot be determined because there is no implicit conversion between 'System.DBNull' and 'System.DateTime?'". My supporting code is basically...

private DateTime? m_dtLastLogin;
m_dtLastLogin = null;
DataRow drow;
drow = m_oDS.Tables["Users"].Rows[0];

有人可以帮忙我这里的短手吗?

Can someone help me with the short-hand here?

推荐答案

嗯,错误信息很清楚。编译器需要确定整个x?y:z表达式的类型。如果y和z具有相同的类型,则很容易。如果y可转换为z,则表达式的类型为z的类型,同样,如果z可转换为y,则类型为y的类型。

Well, the error message is pretty clear. The compiler needs to determine the type of the whole x?y:z expression. If y and z have the same type, it's easy. If y is convertible to z, the type of the expression is that of z, likewise if z is convertible to y the type is that of y.

在你的情况下,y的类型是DBNull,y的类型是m_dtLastLogin的类型(可能是日期时间)。这两种类型不能相互转换,并且没有公共基类型(Object除外),因此编译器不知道该怎么做。

In your case the type of y is DBNull, the type of y is the type of m_dtLastLogin (probably a datetime). Those two types cannot be converted into each other and have no common base type (except for Object), so the compiler doesn't know what to do.

但是,您可以通过将y或z强制转换为对象来帮助编译器:

You can help the compiler, however, by casting either y or z to object:

drow["LastLogin"] = (m_dtLastLogin == null) ? (object)System.DBNull.Value : m_dtLastLogin;

drow["LastLogin"] = (m_dtLastLogin == null) ? System.DBNull.Value : (object)m_dtLastLogin;

这样整个表达式都有类型对象,然后可以将其赋值给卓尔[ LastLogin]

This way the whole expression has the type object, which can then be assigned to drow["LastLogin"].

参考:
C#语言规范 - http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf

第14.13节,条件运算符

Section 14.13, Conditional operator

引用


第二和第三个操作数?:运算符控制条件表达式的类型。设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是相同的类型,那么这就是条件表达式的类型。

  • If X and Y are the same type, then this is the type of the conditional expression.

否则,如果从X到Y存在隐式转换(第13.1节),而不是从Y到X,则Y是条件表达式的类型。

Otherwise, if an implicit conversion (§13.1) exists from X to Y, but not from Y to X, then Y is the type of the conditional expression.

否则,如果从Y到X存在隐式转换(第13.1节),而不是从X到Y,则X是条件表达式的类型。

Otherwise, if an implicit conversion (§13.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.

这篇关于单行if语句生成强制转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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