是的ReferenceEquals(NULL,OBJ)同样的事情空== OBJ? [英] Is ReferenceEquals(null, obj) the same thing as null == obj?

查看:239
本文介绍了是的ReferenceEquals(NULL,OBJ)同样的事情空== OBJ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是不是同样的事情

 如果(的ReferenceEquals(NULL,OBJ))返回false?; 

 如果(空== OBJ)返回false; 


解决方案

您通常会看到这一点的一个实施==操作符



例如:

 公共静态布尔运算符==(富F1,F2富)
{
如果(的ReferenceEquals(F1,F2))
{
返回真;
}
如果(的ReferenceEquals(F1,NULL)||的ReferenceEquals(F2,NULL))
{
返回FALSE;
}
//现在做的比较
}

您别不想使用:

 如果(F1 == F2)

,因为这将递归到相同的代码!另一种方法是:



  IF((对象)F1 ==(对象)F2)

(与同为空检查。)


Is it the same thing?

if (ReferenceEquals(null, obj)) return false;

and

if (null == obj) return false;

解决方案

You'd usually see this in the implementation of an == operator.

For instance:

public static bool operator ==(Foo f1, Foo f2)
{
    if (ReferenceEquals(f1, f2))
    {
        return true;
    }
    if (ReferenceEquals(f1, null) || ReferenceEquals(f2, null))
    {
        return false;
    }
    // Now do comparisons
}

You don't want to use:

if (f1 == f2)

because that will recurse into the same code! An alternative is:

if ((object)f1 == (object)f2)

(And the same for the null check.)

这篇关于是的ReferenceEquals(NULL,OBJ)同样的事情空== OBJ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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