本文介绍了询问数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据类型为LONG的变量。是否有可能传递的值将变为空值,如?



以下是我的代码,



  private   long  UnitHeaderId; 

if (UnitHeaderId!= 0
{
// 在这里做点什么
}
else
{
// 在这里做点什么
}





如果UnitHeaderId为空或空(尽管可能发生的可能性很小), IF语句仍然执行还是直接执行ELSE语句?谢谢。

解决方案



所以尝试将其更改为

 long UnitHeaderId = null; 

它仍然无法编译

现在尝试伪造一种在运行时将 null 转换为变量的方法。这将编译

  long  UnitHeaderId; 
? x = null ;
UnitHeaderId =( long )x;

if (UnitHeaderId!= 0

但是一旦你运行它就会得到运行时异常



所以为了回答你的问题,IF和OR子句都不会被执行 - 如果你以某种方式设法将null变为 UnitHeaderId 你将得到一个异常抛出

Hi, I have a variable which is in data type LONG. Is there any possibility the value being passed will turn in null value such as ""?

Below are my codes,

private long UnitHeaderId;

if(UnitHeaderId != 0)
{
   //do something here
}
else
{
   //do something here
}



If the UnitHeaderId is null or empty(although with very low possibility that it would happen), will the IF statement still get executed or would it straight execute the ELSE statement? Thank you.

解决方案



So try changing it to

long UnitHeaderId = null;

It still won't compile

Now try faking a means of getting null into the variable at run-time... this will compile

long UnitHeaderId;
long? x = null;
UnitHeaderId = (long)x;

if(UnitHeaderId != 0)

but as soon as you run it you get the runtime exception


So to answer your question, neither the IF nor the OR clause will be executed - if you somehow manage to get a null into UnitHeaderId you will get an exception thrown


这篇关于询问数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 05:36