在我的 VS 2015 编译器上,我测试了它
static void Main(string[] args)
{
string str1 = null;
string str2 = null;
if(str1==str2) //they are the same on my machine
{
}
}
但这是记录在案的行为? NULL
根据定义,是未定义的行为,因此将 NULL
与另一个 NULL
进行比较 可能 未定义。可能会发生在我的机器上,使用我当前的 .Net 框架,两个 NULL
结果是相同的。但在未来,它们可能不再相同。
在那种情况下,我的代码会悄无声息地崩溃。
始终假设上述两个 NULL
字符串始终相同是否安全?
最佳答案
是的,已记录 here
If both a and b are null, the method returns true.
当你使用==
时会用到这个方法,提到here .
calls the static
Equals(String, String)
method
关于c# - String NULL 是否总是等于 C# 中的另一个 String NULL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51608812/