当提供的字符串不为空时,string.IsNullOrEmpty 返回 true [英] string.IsNullOrEmpty returns true when supplied string is not null

查看:45
本文介绍了当提供的字符串不为空时,string.IsNullOrEmpty 返回 true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单元测试,它对传入字符串的对象调用方法.

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.

然而,无论 filePath 的值是什么,对 string.IsNullOrEmpty 的调用都是正确的.见下图.

However, no matter what the value of filePath is, the call to string.IsNullOrEmpty is true. See image below.

我在这里遗漏了什么吗?

Am I missing something here?

检查 null 和 string.Empty 分别按预期工作:

Checking for null and string.Empty separately works as expected:

编辑 2:

我已经清理了解决方案,通过文件系统删除了 bin 目录,并且在重建之后,调试器仍然显示应该抛出 ArgumentNullException,尽管它实际上没有被抛出.

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.

推荐答案

filePath 的内容绝对不为空(也不为空),所以我们有两个选择:

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 (i.e. global variable) named filePath, which is empty on null
  • Your debugger is referencing an older version of the binaries. In that case, claear and rebuild the solution

更新

你的问题更新让我觉得第二个选项(上面的)是一个

Your question update makes me think the second option (of the above) is the one

这篇关于当提供的字符串不为空时,string.IsNullOrEmpty 返回 true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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