为什么我不能将double初始化为null [英] Why I cannot initialize double as null

查看:317
本文介绍了为什么我不能将double初始化为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在程序中将double类型初始化为null



Hi,

I am initializing double type as null in my program

double utilization1=null;
            double utilization2=null;
            double utilization3=null;
            double utilization4 = null;







我在我的计划中使用以下条件






And I am using a condition as below in my program

if ((utilization1 != null) && (utilization2!=null)&&(utilization3 != null) && (utilization4!=null))
           {

            //do something
           }





我收到如下错误





I am getting error like below

Cannot convert null to 'double' because it is a non-nullable value type	







任何人都可以帮我解决这个问题




Can anyone help me in solving this problem

推荐答案

默认情况下,您会发现值类型不是可空类型(引用类型是)。要使它成为可空类型,您将声明double的方式更改为 double?。注意类型声明后的问号 - 这是将类型定义为可空的简写方式。这实际上是相当于使用 System.Nullable< double> generic来创建一个可以为空的 double
You'll find that the value types are not nullable types by default (reference types are). To make it a nullable type, you change the way you declare double to be double?. Notice the question mark after the type declaration - this is the shorthand way of defining a type as nullable. What this is actually doing is the equivalent of using the System.Nullable<double> generic to create a nullable type of double.


'double'是值类型,值类型不能包含空值。

如果你想用null值初始化变量那么你应该使用'double ?'而不是双倍。



加倍? utilization1 = null;



if(utilization1.hasvalue)

{

//做某事
}
'double' is value type and value type cannot contain the null value.
If you want initialize variable with null value then you should use 'double?' instead of double.

double? utilization1=null;

if (utilization1.hasvalue)
{
//do something
}


除了使用可空的 double?之外,您还可以使用NaN(非数字):

http:// msdn。 microsoft.com/en-us/library/system.double.nan(v=vs.110).aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.double.isnan (v = vs.110).aspx [ ^ ],

另见 http://msdn.microsoft.com/en-us/library/system.double% 28v = vs.110%29.aspx [ ^ ]。



在大多数情况下,这是最合适的方法,与使用可空类型。



-SA
In addition to using nullable double?, you can also use NaN (Not a Number):
http://msdn.microsoft.com/en-us/library/system.double.nan(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.double.isnan(v=vs.110).aspx[^],
see also http://msdn.microsoft.com/en-us/library/system.double%28v=vs.110%29.aspx[^].

In most cases, this is the most adequate approach, lightweight compared to using the nullable type.

—SA


这篇关于为什么我不能将double初始化为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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