询问数据类型 [英] Inquiry about data type

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

问题描述

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



以下是我的代码,



  private   long  UnitHeaderId; 

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





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

解决方案

你声明的变量UnitHeaderId永远不能为空。

它的默认值是0.



如果你想拥有一个可以为空的变量,你必须这样声明:

 ? UnitHeaderId; 



问号表示该变量可以为空。

有关可空类型的更多信息,请访问:

https://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx [ ^ ]


基本类型(如long,int,short和它们的无符号对应物ulong,int,ushort等)不能为null,设计。



因此,在你的代码中,else部分的唯一方法是当UnitHeaderId等于零时。 UnitHeaderId永远不会为null。处理基本类型的空值的唯一方法是将它们与 Nullable< T> 类型一起使用。这样:

 Nullable< long> nullableUnitHeaderId; 
//
long ? nullableUnitHeaderId;
< / long >


这是你可以很容易找到的东西......



尝试代码,因为它不会编译 -

Quote:

使用未分配的局部变量'UnitHeaderId'

所以尝试将其更改为

 long UnitHeaderId = null; 

它仍然无法编译

Quote:

无法将null转换为'long',因为它是一个不可为空的值类型

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

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

if (UnitHeaderId!= 0

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

Quote:

Nullable对象必须有一个值。



所以为了回答你的问题,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.

解决方案

The variable "UnitHeaderId" you declared can never be null.
It's default value is 0.

If you want to have a nullable variable you must declare it like this:

long? UnitHeaderId;


The question mark indicates that the variable is nullable.
More information about nullable types can be found here:
https://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx[^]


Primitives types (like long, int, short, and their unsigned counterparts ulong, int, ushort, etc.) cannot be null, by design.

So, in your code, the only way for the else part to be reached would be when UnitHeaderId equals zero. UnitHeaderId will never be null. The only way to handle null values for primitive types is to use them with Nullable<T> type. This way:

Nullable<long> nullableUnitHeaderId;
// OR
long? nullableUnitHeaderId;
</long>


This is something you can quite easily find out for yourself...

Try the code as it stands ... it won't compile -

Quote:

Use of unassigned local variable 'UnitHeaderId'

So try changing it to

long UnitHeaderId = null;

It still won't compile

Quote:

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

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

Quote:

Nullable object must have a value.


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


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

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