简体   繁体   English

当提供的字符串不为null时,string.IsNullOrEmpty返回true

[英]string.IsNullOrEmpty returns true when supplied string is not null

I have a unit test that calls a method on an object passing in a string. 我有一个单元测试,它调用传递字符串的对象上的方法。

One of the first things that the method being called does is to check the string for null or empty. 被调用方法的第一件事就是检查字符串是否为null或为空。

However, no matter what the value of filePath is, the call to string.IsNullOrEmpty is true. 但是,无论filePath的值是什么,对string.IsNullOrEmpty的调用都为true。 See image below. 见下图。

为什么字符串为null或为空

Am I missing something here? 我在这里错过了什么吗?

EDIT: 编辑:

Checking for null and string.Empty separately works as expected: 检查null和string.Empty分别按预期工作:

在此输入图像描述

EDIT 2: 编辑2:

I have cleaned the solution, deleted the bin directory via the file system and still, after a rebuild, the debugger shows that the ArgumentNullException should be being thrown, although it actually is not being thrown . 我已经清理了解决方案,通过文件系统删除了bin目录,并且在重建之后,调试器显示应该抛出ArgumentNullException,尽管它实际上没有被抛出

The contents of filePath are definitely not null (and not empty), so that leaves us with two options: filePath的内容绝对不是null(而不是空),因此我们有两个选择:

  • You have a wider scope variable (ie global variable) named filePath , which is empty on null 您有一个更宽的范围变量(即全局变量),名为filePath ,在null时为空
  • Your debugger is referencing an older version of the binaries. 您的调试器正在引用旧版本的二进制文件。 In that case, claear and rebuild the solution 在这种情况下,claear并重建解决方案

Update 更新

Your question update makes me think the second option (of the above) is the one 你的问题更新让我觉得第二个选项(上面的选项)是一个

I'm also experiencing this in Visual Basic with If String.IsNullOrEmpty(foo) . 我也在Visual Basic中使用If String.IsNullOrEmpty(foo)体验这一点。 I agree with Andrei's comment, this seems to be a bug with how the debugger is visualizing this particular construct. 我同意Andrei的评论,这似乎是调试器如何可视化这个特定结构的错误。

Clean/Rebuild did not affect it. 清洁/重建不会影响它。 Interestingly, if you add a more complex body to your If statement, the debugger will only pause (ie yellow arrow) on the last line of the body. 有趣的是,如果向If语句添加更复杂的主体,调试器将仅在主体的最后一行暂停(即黄色箭头)。 It does not actually execute the line of code. 它实际上并没有执行的代码行。

It would be interesting if someone could gives us some insight as to why this happens. 如果有人能给我们一些关于为什么会发生这种情况的见解会很有趣。

Here is the code where I'm seeing this: 这是我看到的代码:

在此输入图像描述

Notice the debugger arrow is on the line e.Cancel = True , even though myItem.Subject is not null. 请注意,调试器箭头位于e.Cancel = True ,即使myItem.Subject 不为 null。 When I press F10, the arrow will advanced to the ElseIf statement and e.Cancel will still be False. 当我按F10时,箭头将前进到ElseIf语句, e.Cancel仍然是False。

Also, while the debugger is on this line, I cannot drag the arrow to a different line like I usually can. 此外,虽然调试器在这一行,我不能像往常一样将箭头拖动到不同的行。 If I attempt to move to another line by dragging the yellow arrow, I get the following error: 如果我尝试通过拖动黄色箭头移动到另一行,我会收到以下错误:

在此输入图像描述

This is the first time I have come across this issue. 这是我第一次遇到这个问题。

The way we stepped round it for now is to add the following on the back of the IF statement. 我们现在采取的方式是在IF语句的背面添加以下内容。

#if DEBUG
            else
            {
                // A hack to fix the debugger issue on the IsNullOrEmpty statement above.
            }
#endif

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

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