可能重复:
What is the difference between these (bCondition == NULL) and (NULL==bCondition)?
我看到了两种方式的比较。是表现上的差异,还是仅仅是个人的偏好?
我在这个答案中看到了它的用法:
What are your favorite extension methods for C#? (codeplex.com/extensionoverflow)
发布于 2011-08-10 15:18:27
在这种情况下,它在C#中的个人偏好。

尤达条件
发布于 2011-08-10 15:18:38
这是C++ days的雏形,在那里您可以使用=而不是==来意外地赋值变量,而且它仍然会通过编译器,因为您可以将几乎任何东西传递到C++中的比较中。不要在C#中使用它,因为它不会允许您这样做。
有效C++:
if (p = NULL) // p gets assigned NULL and result is compared to 0
无效的C#:
if (p = null) // can only use booleans in test
发布于 2011-08-10 15:21:53
使用null == x可以防止意外错误地输入和遗漏一个等于符号,从而分配而不是测试一个值。您将最终得到不编译的null = x。
无论如何,使用Object.ReferenceEquals(x, null)可能更好,因为它可以防止==被重载的情况。
https://stackoverflow.com/questions/7013263
复制相似问题