当一个 Nullable 类型(转换为对象时)为空时,是否可能检测到它?
由于 Nullable<T> 实际上是一个结构体,我认为这应该是可能的。
double? d = null;
var s = GetValue(d); //I want this to return "0" rather than ""
public string GetValue(object o)
{
if(o is double? && !((double?)o).HasValue) //Not working with null
return "0";
if(o == null)
return "";
return o.ToString();
}
object
类型参数的例程被传递了一个Nullable<double>
类型的变量,那么该例程将会收到一个空对象引用(如果变量为 null)或者对System.Double
的引用(如果该变量不为 null)。它不会封箱Nullable<System.Double>
的实例。 - supercat