.NET的DBNull VS在所有变量类型没有什么? [英] .NET DBNull vs Nothing across all variable types?

查看:197
本文介绍了.NET的DBNull VS在所有变量类型没有什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对空值和变量在.NET有点困惑。 (VB preferred)

I am a little confused about null values and variables in .NET. (VB preferred)

有没有什么办法来检查任何特定变量的NULL的含量无论它是一个对象或值类型?还是我的空支票必须始终预见无论是检查值类型(如System.Integer)或对象?

Is there any way to check the "nullness" of ANY given variable regardless of whether it was an object or a value type? Or does my null check have to always anticipate whether it's checking a value type (e.g. System.Integer) or an object?

我猜我寻找的是一个函数,检查所有可能的类型的空内斯。也就是说,任何类型的变量

I guess what I'm looking for is a function that checks all possible kind of null-ness. That is, any type of variables that

一)从未被分配一个值,因为声明

a) were never assigned a value since declared

二)被分配从数据对象(即来自一个数据库)

b) were assigned a null value from a data object (that came from a database)

C)分别设置等于这是空的另一个变量值

c) were set equals to another variable value which was null

d)项设置为从未设置过的或过期的ASP.NET会话/应用程序变量。

d) were set to an ASP.NET session/application variable that was never set or expired.

有一个一般的最佳实践,当涉及到处理空的场景在.NET?

Is there a general best-practice when it comes to handling null scenarios in .NET?

先谢谢您的任何反馈!

更新:当我谈到一个值类型为空,我真正的意思是,要么从未设置或者是在某个点设定为等于或铸造而成的空值类型目的。

UPDATE: When I talk about a value type being "null", what I really mean is a value type that was either never set or was at some point set equal to or cast from a null object.

推荐答案

普通的值类型(布尔型,整型,长整型,浮点型,双枚举和结构)是不能为空。

Normal value types (booleans, ints, longs, float, double, enum and structs) are not nullable.

对于所有的值类型的默认值为0。

The default value for all value types is 0.

在CLR不会让你访问的变量,除非他们已经确定。你可能会认为这并非总是如此,但有时CLR步骤和初始化它们。在方法层面,你必须明确地初始化所有变量在使用之前。

The CLR won't let you access variables unless they have been set. You may think this isn't always the case, but sometimes the CLR steps in and initializes them for you. At a method level you must explicitly initialize all variables before they are used.

此外,由于其他人指出,因为.NET 2.0有一个名为可空℃的新的通用型; T> 。有在C#中如int一些编译器速记?手段可空< INT> ,翻倍?手段可空<双>

Further, as others point out, since .net 2.0 there is a new generic type called Nullable<T>. There are some compiler shorthands in C# like int? means Nullable<int>, double? means Nullable<double> etc.

您只能换行可空&LT; T&GT; 在非空值类型,这是很好的,因为引用已经为空的能力

You can only wrap Nullable<T> over non-nullable value types, which is fine since references already have the ability to be null.

int? x = null;

对于一个int?,同时可以测试的空,它有时更好打电话 x.HasValue()

在C#中另外还有可空凝聚运营商?当你想要一个可空分配给非空值类型。但是,如果你没有操作,你可以调用GetValueOrDefault()。

In C# there's also the nullable coalescing operator ?? when you want to assign a nullable to a non-nullable value type. But if you don't have the operator, you can call GetValueOrDefault().

int y = x ?? 2; // y becomes 2 if x is null.
int z = x.GetValueOrDefault(2); // same as y

这篇关于.NET的DBNull VS在所有变量类型没有什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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