在可为空的类型上使用合并null运算符可更改隐式类型 [英] using coalescing null operator on nullable types changes implicit type
问题描述
我希望接下来的三行代码是相同的:
I would expect the next three lines of code to be the same:
public static void TestVarCoalescing(DateTime? nullableDateTime)
{
var dateTimeNullable1 = nullableDateTime.HasValue ? nullableDateTime : DateTime.Now;
var dateTimeNullable2 = nullableDateTime != null ? nullableDateTime : DateTime.Now;
var dateTimeWhatType = nullableDateTime ?? DateTime.Now;
}
在所有情况下,我都将nullableDateTime
分配给新变量.我希望所有变量的类型都变为DateTime?
,因为那是nullableDateTime
的类型.但是令我惊讶的是,dateTimeWhatType
的类型变为DateTime
,所以不能为空.
In all cases, I assign nullableDateTime
to the new variable. I would expect the type of all variables to become DateTime?
since that is the type of nullableDateTime
. But to my surprise, the type of dateTimeWhatType
just becomes DateTime
, so not nullable.
为了使情况更糟,ReSharper建议用空合并表达式替换第二条语句,将其转换为表达式3.因此,如果我让ReSharper执行其操作,则变量的类型将从DateTime?
更改为
To make things worse, ReSharper suggests to replace the second statement with a null coalescing expression, turning it into expression 3. So if I let ReSharper do its thing, the type of the variable will change from DateTime?
to DateTime
.
实际上,假设在该方法的其余部分中,我将使用
In fact, let's say that in the remainder of the method, I would use
if (someCondition) dateTimeNullable2 = null;
这样编译就可以了,直到我让ReSharper用空合并版本替换第二个表达式为止.
That would compile just fine, until I let ReSharper replace the second expression with the null coalescing version.
AFAIK,替换
somevar != null ? somevar : somedefault;
与
somevar ?? somedefault;
确实应该产生相同的结果.但是对于隐式键入可为空的类型,编译器似乎威胁??
,就像它的意思一样.
should indeed produce the same result. But for implicit typing on a nullable type, the compiler seems to threat ??
as if it means.
somevar != null ? somevar.Value : somedefault;
所以我想我的问题是,为什么在使用??
时隐式类型会被更改,以及在文档中可以找到有关此信息的位置.
So I guess my question is why the implicit type is changed when I use ??
, and also where in the documentation I could find info on this.
顺便说一句,这不是现实情况,但是我想知道为什么使用??
会更改(隐式)类型.
BTW, this isn't a real world scenario, but I would like to know why using ??
changes the (implicit) type.
推荐答案
您的前两个示例使您误入歧途;最好是不要考虑您的
Your first two examples are leading you astray; better would be to consider not your
var dateTimeNullable1 = nullableDateTime.HasValue
? nullableDateTime
: DateTime.Now;
但是
var dateTimeNullable1 = nullableDateTime.HasValue
? nullableDateTime.Value
: DateTime.Now;
引用C#3.0规范的第7.12节空合并运算符"(对有些粗略的格式表示歉意):
To quote section 7.12 "The null coalescing operator" of the C# 3.0 spec (apologies for slightly ropey formatting):
表达式
a ?? b
的类型取决于哪个隐式 在操作数类型之间可以进行转换.为了 首选a ?? b
的类型为A
0
,A
或B
, 其中A
是a
的类型,B
是b
的类型(前提是b
具有类型),并且A
0
是A
的基础类型,如果A
是可为空的类型,否则为A
.
The type of the expression
a ?? b
depends on which implicit conversions are available between the types of the operands. In order of preference, the type ofa ?? b
isA
0
,A
, orB
, whereA
is the type ofa
,B
is the type ofb
(provided thatb
has a type), andA
0
is the underlying type ofA
ifA
is a nullable type, orA
otherwise.
因此,如果a
是Nullable<Something>
,并且b
可以隐式转换为Something
,则整个表达式的类型将是Something
.正如@Damien_The_Unbeliever所建议的那样,此运算符的 point 要合并空值!
So if a
is Nullable<Something>
, and b
can be implicitly converted to Something
, the type of the whole expression will be Something
. As @Damien_The_Unbeliever suggests, the point of this operator is to coalesce nulls!
这篇关于在可为空的类型上使用合并null运算符可更改隐式类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!