Abe*_*bel 10 .net c# vb.net vb.net-to-c#
在混合代码项目(VB和C#)中,我们正在调试一些旧的Visual Basic代码,如下所示:
If Request.Params("xxx") <> "" Then
'do something
Run Code Online (Sandbox Code Playgroud)
我认为这是一个错误,因为Request.Params可能是null,在这种情况下,语句将变为错误,这不是主意.
所以我认为.我刚刚再次发现,VB Nothing和C#的null东西Nothing不一样,null而且不一样.事实上:
if(String.Empty == null) // in C# this is always false (correct)
Run Code Online (Sandbox Code Playgroud)
If String.Empty = Nothing Then ' in VB this is always true (????)
Run Code Online (Sandbox Code Playgroud)
这怎么可能呢?这是一些向后兼容问题吗?
Jon*_*eet 16
Nothing在VB中对字符串有特殊含义.要测试字符串引用是否为null,您需要:
If value Is Nothing
Run Code Online (Sandbox Code Playgroud)
数值比较将Nothing视为0.字符串比较将Nothing视为""(空字符串).
我怀疑这只是为了向后兼容VB6 - 如果我是VB开发人员的话,这不是我会满意的.
形式的比较
If value = Nothing
Run Code Online (Sandbox Code Playgroud)
编译为一个调用,Microsoft.VisualBasic.CompilerServices.Operators.CompareString如果一个操作数为空,另一个为空,则返回0(即相等).