.NET DBNull 与所有变量类型中的 Nothing 比较?
我对 .NET 中的空值和变量有点困惑。 (首选VB)
有没有办法检查任何给定变量的“空性”,无论它是对象还是值类型? 或者我的 null 检查是否必须始终预测它是在检查值类型(例如 System.Integer)还是对象?
我想我正在寻找的是一个检查所有可能类型的空性的函数。 也就是说,任何类型的变量
a) 自声明后从未被赋值
b) 被从数据对象(来自数据库)分配空值
c) 被设置为等于另一个空值
d) 是设置为从未设置或过期的 ASP.NET 会话/应用程序变量。
在 .NET 中处理 null 场景时是否有通用的最佳实践?
更新:当我谈论“null”值类型时,我真正的意思是从未设置过或在某个时刻设置为等于或从 null 对象转换的值类型。
I am a little confused about null values and variables in .NET. (VB preferred)
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) were set equals to another variable value which was null
d) were set to an ASP.NET session/application variable that was never set or expired.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
值类型不能为空。 它违反了值类型的含义。 您可以将值类型包装为 Nullable(Of T),这为您提供了一组很棒的方法,并检查 Nothing 是否有效。 但是这个包装器确实会带来很多开销。 也许你可以澄清你想做什么?
为了完整起见,可空包装器的 VB 语法是:
编辑:值类型始终预初始化为默认值,数字为 0,布尔值为 false,等等。
Value Types can't be null. It violates what it means to be a Value Type. You can wrap Value Types as Nullable(Of T) which gives you a great set of methods, and checks for Nothing do work. But you do have a lot of overhead with that wrapper. Perhaps you can clarify what you're trying to do?
For completeness the VB syntax for Nullable wrappers is:
EDIT: Value Type are always preinitialized to a default value, 0 for numerics, false for Booleans, etc.
普通值类型(布尔值、整数、长整型、浮点型、双精度型、枚举和结构)不可为空。
所有值类型的默认值为 0。
除非已设置变量,否则 CLR 不会允许您访问变量。 您可能认为情况并非总是如此,但有时 CLR 会介入并为您初始化它们。 在方法级别,您必须在使用所有变量之前显式初始化它们。
此外,正如其他人指出的那样,自 .net 2.0 以来,出现了一种新的泛型类型,称为 Nullable。 C#中有一些像int这样的编译器简写吗? 意味着
Nullable
,双精度? 意味着Nullable
等。您只能将
Nullable
包装在不可为 null 的值类型上,这很好,因为引用已经可以为 null。对于 int?,虽然您可以针对 null 进行测试,但有时调用 x.HasValue() 会更好。
在 C# 中还有 可空合并运算符 ?? 当您想要将可为空的值分配给不可为空的值类型时。 但如果您没有该运算符,则可以调用 GetValueOrDefault()。
Normal value types (booleans, ints, longs, float, double, enum and structs) are not nullable.
The default value for all value types is 0.
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.
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? meansNullable<int>
, double? meansNullable<double>
etc.You can only wrap
Nullable<T>
over non-nullable value types, which is fine since references already have the ability to be null.For an int?, while you can test against null, it's sometimes nicer to call
x.HasValue()
.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().
这就是你所追求的吗?
事实上,我不确定你为什么想要这种结构。 我唯一一次检查 DBNULL.Value 是当我使用来自数据库的值时,并且在我将 DATA 命名空间类中的所述值分配给其他类之前 [即 dim b as string = dataReader(0) ]。
通常,如果您担心某个对象尚未实例化,或者需要重新实例化它,那么只需 IsNothing 检查就足够了。
Is this what you're after?
In truth I'm not certain why you'd wish for this structure. The Only time I'd check for DBNULL.Value is when I'm using values that came from a database, and before I assign said value from a DATA Namespace class to some other class [i.e. dim b as string = dataReader(0)].
Typically, if you're concerned about an object having not been instantiated, or needing it to be re-instantiated, then just an IsNothing check will suffice.
在.Net中,据我所知,只有两种类型的null:null(VB中没有任何内容)和DbNull。 如果您使用的是 System.Nullable,则可以使用与对象相同的空检查语法。 如果您的可为 null 的对象被装箱,.Net 2.0 CLR 足够智能,能够找出处理此问题的正确方法。
我遇到这两种类型的唯一情况是在应用程序的数据层中,我可能会直接访问数据库数据。 例如,我在 DataTable 中遇到了 DbNull。 要在这种情况下检查这两种空类型,您可以编写一个扩展方法,例如(抱歉,在 C# 中):
In .Net that are only two types of null that I am aware of, null (nothing in VB) and DbNull. If you are using a System.Nullable, you can use the same null checking syntax as you would with an object. If if your nullable object is boxed the .Net 2.0 CLR is smart enough to figure out the right way to handle this.
The only case I have run into both types is in the data tier of an application where I might be accessing database data directly. For example, I have run into DbNull in a DataTable. To check for both of these null types in this situration, you could write an extension method like (sorry, in C#):
值类型变量不能包含null,因为null意味着,null意味着引用指向任何地方。 我不知道在 VB.net 上,但在 c# 上,您可以使用“?”将值类型包装为可为空,例如:
Value type variables can't contain null, that's because what null means, null means that the references points nowhere. I don't know on VB.net but on c# you can wrap value types to be nullables using the "?", like:
只要您使用 Option Strict On 进行开发,(a) 就不应该成为问题。 编译器会骂你的。 如果您担心检查参数,只需使用
For (b),您的数据库交互层应该处理这个问题。 如果您使用 LINQ,则有多种方法可以处理此问题。 如果您使用类型化数据集,则自动生成的行上有一个 .IsMyVariableNull 属性。
对于 (c),您无需担心值类型,但可以使用简单的 Is Nothing(或 IsNot Nothing)来检查引用类型。
对于 (d),您可以在读取后应用相同的逻辑。 针对 Nothing 测试接收变量。
在大多数情况下,简单地检查一下 Is Nothing 就可以了。 您的数据库交互层将帮助您处理数据中空值的棘手情况,但这取决于您是否适当地处理它们。
As long as you're developing with Option Strict On, (a) shouldn't be an issue. The compiler will yell at you. If you're worried about checking for parameters, just use
For (b), your database interaction layer should handle this. If you're using LINQ, there are ways to handle this. If you're using typed data sets, there an .IsMyVariableNull property on the row that gets auto-generated.
For (c), you don't need to worry about value types, but reference types can be checked with a simple Is Nothing (or IsNot Nothing).
For (d), you can apply the same logic after the read. Test the receiving variable against Nothing.
For the most part, a simple check of Is Nothing will get you by. Your database interaction layer will help you handle the stickier case of null values in your data, but it's up to you to handle them appropriately.