C#:与 null 比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/593560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 09:35:27  来源:igfitidea点击:

C#: Comparing with null

c#null

提问by

Are these equivalent:

这些是等价的:

if (null==myobject)
{
//do something 
}

and

if (myobject==null)
{
//do something 
}

or will they produce different code?

或者他们会产生不同的代码?

回答by JaredPar

In the 99% case this code is equivalent.

在 99% 的情况下,此代码是等效的。

The one exception is when the type of the object in question overrides the == operator. It's possible for a bug to be introduced in == which causes problems when one parameter is null.

一个例外是当所讨论的对象的类型覆盖 == 运算符时。== 中可能会引入一个错误,当一个参数为空时会导致问题。

A particular example I've seen before is the following

我以前见过的一个特定示例如下

public static bool operator==(Student left, Student right) {
  return left.Equals(right);
}

This will fail when null is on the left, but likely not when null in on the right.

当 null 在左边时这将失败,但当 null 在右边时可能不会。

This is a pretty far out corner case though.

不过,这是一个相当遥远的角落案例。

回答by ReinstateMonica Larry Osterman

The form of "if" statement that puts the constant at the left is a holdover from C/C++ where you could have an arbitrary expression in an if statement.

if将常量放在左侧的 " " 语句的形式是 C/C++ 的保留,您可以在 if 语句中使用任意表达式。

C#'s syntax for if statements requires that the expression evaluate to a bool which means that if (foo = 0)won't compile.

C# 的 if 语句语法要求表达式计算为 bool,这意味着if (foo = 0)不会编译。

回答by Kenneth Cochran

As pointed out by others they are mostly equivalent.

正如其他人所指出的,它们大多是等效的。

You should also take a look at: http://en.wikipedia.org/wiki/Null_Object_pattern

您还应该看看:http: //en.wikipedia.org/wiki/Null_Object_pattern

It is a very useful alternative to simply checking for a null reference.

这是简单检查空引用的非常有用的替代方法。

回答by Kenneth Cochran

The

if (null==myobject) {

is a safe way of writing an if statement. It comes from C/C++ where the condition is an expression evaluated to an int. If the result is zero that means false, anything else is true. You could write something like

是编写 if 语句的安全方式。它来自 C/C++,其中条件是评估为int. 如果结果为零,那意味着false,其他任何事情都是true。你可以写类似的东西

if (variable == 1) {

but if you weren't careful you could also write

但如果你不小心你也可以写

if (variable = 1) { 

in which case you have an assignment that always evaluates to 1 and thus is always true.

在这种情况下,您的赋值始终为 1,因此始终为真。

You could compile this and run it with no problems, but the result wouldn't be what you expected. So C/C++ programmers started to write things like

你可以编译它并毫无问题地运行它,但结果不会是你所期望的。所以 C/C++ 程序员开始写这样的东西

if (1 == variable) {

This won't compile if you misspell it, so you always had to write it as you meant to write it. This later becomes a (good) habit and you use it in all the languages you program with, C# for example.

如果您拼错了它,它将无法编译,因此您总是必须按照您的意图编写它。这后来成为一种(好)习惯,您可以在使用的所有语言(例如 C#)中使用它。

回答by Glenn Slayden

For those who miss it, of if you're looking to reduce clutter, it's also possible to enable C-language-style null-checking for your C#classes:

对于那些错过它的人,如果您想减少混乱,也可以C-language为您的C#类启用-style 空检查:

class MyObj
{
    public void implicit_null_comparison_demo()
    {
        MyObj mo = null;
        // ...

        if (mo)         //  <-- like so (can also use !mo)
        {
            // it's not null...
        }
        else
        {
            // it's null...
        }
    }

    public static implicit operator bool(MyObj mo)
    {
        return (Object)mo != null;
    }
};