条件运算符:可空类型转换 [英] Conditional Operator ?: with Nullable type Casting

查看:220
本文介绍了条件运算符:可空类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从MSDN文档,下面的两个片段是一样的:

From the MSDN documentation, the following two snippets are equal:

bool value;
int x = (value) ? 0 : 1;

bool value;
int x;
if (value)
    x = 0;
else
    x = 1;

大,精彩。我用它所有的时间。简洁而有效的。

Great, wonderful. I use it all the time. Terse and effective.

如果我们尝试用一个可空类型,像这样:

If we try this with a nullable type, like so:

int? x = (value.HasValue) ? value.Value : null;

我们得到一个编译时错误:

We get a compile-time error:

The type of conditional expression cannot be determined
because there is no implicit conversion between '{NullableType}' and null.

这精致编译:

int? value;
int? x;

if (value.HasValue)
    x = value.Value;
else
    x = null;

所以,我明白,编译器需要显式转换为的方式(INT?)空编译第一个语句。我不明白的是为什么它是需要在该声明中,而不是的if else 块。

So, I understand that the compiler requires an explicit cast in the way of (int?)null to compile the first statement. What I don't understand is why it is required in that statement, but not the If Else block.

推荐答案

可以重新present任何基于对象的数据类型。你需要投作为一种数据类型所以它知道你在说什么。

null can represent any object-based datatype. You need to cast null as a datatype so it know what you are talking about.

int? x = (value.HasValue) ? value.Value : (int?)null;

我知道,这听起来有点怪怪的。

I know, it sounds a bit strange.

要回答的问题在意见:

但为什么它是不是暗示?
  是啊,我明白了。但为什么我没有投在一个的if else块?

Why is it not implicit though?
Yeah, I get that. But why do I not have to cast it in a If Else block?

让我们穿行于code。

Let's walk through the code.

其他语句是这样的:

else x = null;

这意味着你要分配的的值 X 。这是有效的,因为 X 诠释?,这需要零点

This means you are assigning the value of null to x. This is valid, because x is a int?, which takes nulls.

的区别在于,当你有三元运算符。它说:运营商的价值分配到 X 。这个问题(和你的错误的原因)是,什么数据类型是三元运算符的结果?

The difference comes when you have the ternary operator. It says: "assign the value of the operator into x". The question (and the reason for your error) is, what datatype is the result of the ternary operator?

从code,你不能肯定,而且编译器会引发其手。

From your code, you can't be sure, and the compiler throws its hands up.

int? x = (value.HasValue) ? value.Value : null;
// int?        bool             int        ??

什么数据类型是?你快说好这是一个诠释?,因为对方是一个 INT ,其结果是诠释?。问题是,怎么样如下:

What datatype is null? You are quick to say "well it's a int?, because the other side is a int and the result is a int?". The problem is, what about the following:

string a = null;
bool? b = null;
SqlConnectionStringBuilder s = null;

这也是有效的,这意味着可用于任何基于对象的数据类型。这就是为什么你必须明确地投如要使用,因为它可以用于任何类型!

This is also valid, which means null can be used for any object-based datatype. This is why you have to explicitly cast null as the type you want to use, because it can be used for anything!

另一种解释(和可能更准确):

Another explanation (and possible more accurate):

您不能有一个可空和非可空值之间的隐式转换。

You can't have an implicit cast between a nullable and a non-nullable value.

INT 不是空的(这是一个结构),其中是。这就是为什么在Habib的答案,你可以把投在任的左侧或右侧。

int is not-nullable (it's a structure), where null is. This is why in Habib's answer you can put the cast on either the left or right side.

这篇关于条件运算符:可空类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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