无法在异常构造函数中写入字符串

Gur*_*epS 2 .net c# visual-studio-2008

当我将字符串传递给异常参数时,Visual Studio似乎抱怨.

if (str1 == null || str2 == null)
{
    throw new ArgumentNullException("lmkl");
}
Run Code Online (Sandbox Code Playgroud)

Visual Studio说它无法解析符号"lmkl".

如果我有一个字符串变量(例如上面的throw new... string s = "test";)并将其作为异常的参数包含在内,那么Visual Studio对此非常满意.

是什么赋予了?

谢谢

Ric*_*ett 7

该重载构造函数的文档ArgumentNullException采用单个字符串参数声明该参数应为:

The name of the parameter that caused the exception.

目前,如果您的代码抛出异常,您将不知道哪个参数为null.

建议改写

if (str1 == null) throw new ArgumentNullException("str1");
if (str2 == null) throw new ArgumentNullException("str2"); 
Run Code Online (Sandbox Code Playgroud)


Mar*_*ell 7

事实上,Visual Studio的不关心这个在所有.我假设你安装了ReSharper?这验证了许多常见错误,包括错误使用模式等ArgumentException.它还有更好的null检查 - 不是很"合同",但仍然非常有帮助.

它只有在能够看到已知模式中使用的字符串文字时才会尝试这种情况 - 分析如何分配变量对于实际分析来说太过分了.