Jon*_*eet 12
它的含义是,如果你这样做:
int? x = 5;
object y = x; // Boxing
Run Code Online (Sandbox Code Playgroud)
你最终得到一个盒装int
,而不是盒装Nullable<int>
.同样,如果你这样做:
int? x = null; // Same as new Nullable<int>() - HasValue = false;
object y = x; // Boxing
Run Code Online (Sandbox Code Playgroud)
然后y结束为空引用.
这意味着,如果你这样做:
int? i = 5;
object o = i;
Run Code Online (Sandbox Code Playgroud)
它是一个装箱的"int"(5),而不是"int?" (5).如果x为null(!HasValue),则o为空,而不是空"int?"周围的框.
然后当你取消装箱:
i = (int?)o;
Run Code Online (Sandbox Code Playgroud)
如果o为null,则变为空"int?"; 否则,5是未装箱的并用于创建"new int?(5)".
基本上,你不应该(没有作弊)能够nullable<T>
直接获得盒装- 只有盒装T