简体   繁体   English

没有什么等于String.Empty,null不等于String.Empty,我在这里缺少什么?

[英]Nothing equals String.Empty, null does not equal String.Empty, what am I missing here?

In a mixed code project (VB and C#) we were debugging some old Visual Basic code like this: 在混合代码项目(VB和C#)中,我们正在调试一些旧的Visual Basic代码,如下所示:

If Request.Params("xxx") <> "" Then
   'do something

I considered this a bug as Request.Params could be null , in which case the statement would've become false which wasn't the idea. 我认为这是一个错误,因为Request.Params可能为null ,在这种情况下,语句将变为false,这不是主意。

So I thought. 所以我认为。 I just found out -- again -- that VB's Nothing and C#'s null are not the same things and Nothing is not the same as null . 我刚刚发现 - VB的Nothing和C#的null不是同一个东西, Nothingnull In fact: 事实上:

if(String.Empty == null)          // in C# this is always false (correct)
If String.Empty = Nothing Then    ' in VB this is always true (????)

How is this even possible? 这怎么可能呢? Is this some backward compatibility issue? 这是一些向后兼容问题吗?

Nothing has a special meaning in VB for strings. VB中的字符串Nothing特殊含义。 To test whether a string reference is null, you need: 要测试字符串引用是否为null,您需要:

If value Is Nothing

From the VB comparison operators documentation : VB比较运算符文档

Numeric comparisons treat Nothing as 0. String comparisons treat Nothing as "" (an empty string). 数值比较将Nothing视为0.字符串比较将Nothing视为“”(空字符串)。

I suspect this is just for backward compatibility with VB6 - it's not something I'd be happy with, if I were a VB developer. 我怀疑这只是为了向后兼容VB6 - 如果我是VB开发人员的话,这不是我会满意的。

A comparison of the form 形式的比较

If value = Nothing

is compiled to a call to Microsoft.VisualBasic.CompilerServices.Operators.CompareString which returns 0 (ie equal) if one operand is null and the other is empty. 被编译为对Microsoft.VisualBasic.CompilerServices.Operators.CompareString的调用,如果一个操作数为空而另一个操作数为空,则返回0(即相等)。

In vb6, the default value for a string variable was an empty string. 在vb6中,字符串变量的默认值是空字符串。 A vb6 programmer relying upon such behavior would be no "worse" than a C programmer relying upon default-zero initialization of int variables; 依赖于这种行为的vb6程序员并不比依赖于int变量的默认零初始化的C程序员“更糟糕”; both behaviors were specified as part of the language. 这两种行为都被指定为语言的一部分。

Further, in COM (the framework upon which previous versions of VB6 were based), any time a reference to string was created, someone would have to manually dispose of it. 此外,在COM(VB6的早期版本所基于的框架)中,每当创建对字符串的引用时,某人将不得不手动处理它。 Since the most commonly used string was the empty string, many COM methods are explicitly documented as regarding a null pointer as equivalent to an empty string. 由于最常用的字符串是空字符串,因此许多COM方法都明确记录为将空指针视为等效于空字符串。 This means that a function returning an empty string or passing one as a value parameter or returning one may simply pass a null pointer without having to allocate anything; 这意味着返回空字符串或将其作为值参数传递或返回一个函数的函数可以简单地传递空指针而不必分配任何内容; the recipient of the null pointer will then not have to de-allocate anything. 然后,空指针的接收者将不必取消分配任何内容。

Because Objects in .net do not require explicit deallocation, the performance advantages of regarding a null reference as an empty string no longer apply. 因为.net中的对象不需要显式释放,所以将空引用作为空字符串的性能优势不再适用。 Nonetheless, methods which are called from code that might expect behavior similar to that of COM methods will often regard null string references as being the same as empty strings. 尽管如此,从代码中调用可能期望类似于COM方法的行为的方法通常会将空字符串引用视为与空字符串相同。

You want 你要

If Not String.IsNullOrEmpty(Request.Params("xxx") Then
    ...
End If

Or 要么

if (!String.IsNullOrEmpty(Request.Params("xxx")) {
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM